How to set up default manifest_json_path while having multiple configurations? - symfony

I'm working on setting up a project for multiple configurations. I know that if you want to have multiple json_manifest_paths, you need modify config.yml like this:
assets:
base_urls:
- "%cdn_url%"
packages:
project_1:
json_manifest_path: '%kernel.project_dir%/web/build/project_1/manifest.json'
projest_2:
json_manifest_path: '%kernel.project_dir%/web/build/project_2/manifest.json'
assets:
version: "%asset_version_generated%"
base_path: "/build"
And then you call your assets like this:
<img src="{{ asset("build/desktop/images/background.png", 'project_1') }}" />
But I also want to have a default json_manifest_path which will be targeted if I call my assets without the second parameter, like this:
<img src="{{ asset("build/images/background.png") }}" />
So, sometimes I'll have the project project_1 built inside /build/project_1 and sometimes I'll have project_2, and so on, you get the idea.
I need to have a third option where a project will be built directly inside /build and the way I call assets will be by not giving it a second argument. In that case, manifest.json path will be directly inside /build.
I tried with this but it's giving me an error that it doesn't exist, even though I am not calling my assets without a second argument:
assets:
json_manifest_path: '%kernel.project_dir%/web/build/manifest.json'
base_urls:
- "%cdn_url%"
packages:
project_1:
json_manifest_path: '%kernel.project_dir%/web/build/project_1/manifest.json'
project_2:
json_manifest_path: '%kernel.project_dir%/web/build/project_2/manifest.json'
assets:
version: "%asset_version_generated%"
base_path: "/build"
TL;DR
How do I set up a default json_manifest_path inside config.yml which will be targeted if I call my assets without the second parameter?

I did something similar with EventListener whitch listen for onKernelController even and adds the necessary assets package like so:
$strategy = new JsonManifestVersionStrategy(
$this->parameterBag->get('kernel.project_dir').'/public/build/'.$your_project_n.'/manifest.json'
);
$this->assetPackages->setDefaultPackage(new Package($strategy));
Then you don’t need to specify the second argument of the asset function at all.
Similarly you can dynamically add the default package and leave the rest hardcoded In the config file

Related

What config when controller is other folder and template path is inadequate

I work with Symfony 4.2
I divided my dir structure, and every part of app is in other folder.
Ex. admin part code is in src/Admin/...
- src/Admin/Controller
etc.
When I set #Template in controller method symfony tell me to situate templates in templates/
But I want to it placed in teplates/Admin/
What I should change in config.
If is other solution than set every action path in #Template(...) ?
Taken from the docs:
# config/packages/twig.yaml
twig:
# ...
paths: ["%kernel.project_dir%/resources/views"]
https://symfony.com/doc/current/configuration/override_dir_structure.html#override-the-templates-directory
This might also be interesting for you:
https://symfony.com/doc/current/templating/namespaced_paths.html#multiple-paths-per-namespace

Nested variables in Symfony .env

I need to use nested variables in Symfony's .env file. I tried this:
# .env
DIRECTORY=my_bucket
PREFIX_URI='cdn.example.com/%env(DIRECTORY)%'
Then I need to use PREFIX_URI in a .yaml configuration file in config/packages:
vich_uploader:
mappings:
fs_name:
uri_prefix: '%env(PREFIX_URI)%'
This doesn't seem to work however. The variables are just ignored. How should I go about it?
Your .env file is not using PHP. The correct way to reference another variable is like this:
DIRECTORY=my_bucket
PREFIX_URI="cdn.example.com/${DIRECTORY}"

NoReverseMatch when using Apphook without namespace

I use Django CMS 3 and Django 1.6 and the default django polls app , and I am doing this short tutorial.
My problem is that the PollsApp works fine when it's using namespace like this:
djangocms_polls/cms_app.py:
...
class PollsApp(CMSApp):
name = _("Poll App")
urls = ["polls.urls"]
app_name = "polls"
...
polls/templates/polls/index.html:
...
{% for poll in latest_poll_list %}
<li>{{ poll.question }}</li>
{% endfor %}
...
But when I delete the "polls:" part from the index.html, it won't work (and it doesn't matter if there is or isn't app_name field in PollsApp) and I get this Error:
NoReverseMatch at /polls/
Exception Value:
Reverse for 'detail' with arguments '(1L,)' and keyword arguments '{}' not found. 0 pattern(s) tried: []
Why am I making my life hard when everything works - you ask? It's because I want to use apps that doesn't use namespaces like django-shop and when I created apphook for django-shop - the same problem occured.
When you're using apps via an Apphook in CMS you have to supply a namespace to when creating URLs in the templates.
In general also, when creating apps I believe it to be from a 'best practice' approach to always namespace your app and template URLs.
If you're using an app which doesn't make use of the app_name Meta attribute, you can define a namespace when you include an app's URLs in your root urls.py. Take a look over the example here; https://docs.djangoproject.com/en/1.7/topics/http/urls/#reversing-namespaced-urls

HearsayRequireJSBundle: Javascript files not found

I am using HearsayRequireJSBundle to implement RequireJS into my Symfony2 project.
In my Twig template I initialize as follows:
{{ require_js_initialize({ 'main' : 'base/main' }) }}
The file that then is being looked for is:
http://myhost.com/app_dev.php/js/base/main.js
This however returns a 404 not found error.
My config looks like this:
hearsay_require_js:
base_directory: %kernel.root_dir%/../src/Acme/Bundle/BaseBundle/Resources/scripts
require_js_src: bundles/acmebase/assets/scripts/require-2.1.5.min.js
paths:
base: '#AcmeBaseBundle/Resources/scripts'
Did I miss anything here? Any help is appreciated.
There's no need to define the path to your base directory twice - the paths configuration node is for extra paths, with base_directory being the default. Try changing your configuration to this:
hearsay_require_js:
base_directory: %kernel.root_dir%/../src/Acme/Bundle/BaseBundle/Resources/scripts
require_js_src: bundles/acmebase/assets/scripts/require-2.1.5.min.js
and alter your template to this (will now be relative to base_directory):
{{ require_js_initialize({ 'main' : 'main' }) }}

Custom grunt.helper values possible?

I am working on my own boilerplate using grunt's CLI possibilities like so grunt init:webdesign-project - for this I created a folder named webdesign-project within node_modules/grunt/init and a corresponding webdesign-project.js file. So far everything works great.
Now I wanted to insert my own "questions" with the grunt.helper function like this
grunt.helper('prompt_for', 'img_path', 'img')
however this gives me
TypeError: Cannot set property 'name' of undefined
at Object.module.exports.grunt.registerHelper.grunt.utils.spawn.cmd (/usr/lib/node_modules/grunt/tasks/init.js:573:17)
at Task.helper (/usr/lib/node_modules/grunt/lib/util/task.js:117:19)
at Object.exports.template (/usr/lib/node_modules/grunt/tasks/init/webdesign-project.js:30:11)
at Object.module.exports.grunt.registerHelper.done (/usr/lib/node_modules/grunt/tasks/init.js:240:27)
at Object.task.registerTask.thisTask.fn (/usr/lib/node_modules/grunt/lib/grunt/task.js:58:16)
at Task.<anonymous> (/usr/lib/node_modules/grunt/lib/util/task.js:341:36)
at Task.start (/usr/lib/node_modules/grunt/lib/util/task.js:357:5)
at Object.grunt.tasks (/usr/lib/node_modules/grunt/lib/grunt.js:143:8)
at Object.module.exports [as cli] (/usr/lib/node_modules/grunt/lib/grunt/cli.js:36:9)
at Object.<anonymous> (/usr/lib/node_modules/grunt/bin/grunt:19:14)
Isn't it possible to define your own variables using this function?
EDIT: Does anybody know if a documentation for this function exists? (Couldn't find one yet)
You shouldn't modify or add files within the node_modules/ folder as they will be overwritten upon updating with npm. Take a look at the init docs for creating custom init templates: https://github.com/gruntjs/grunt/blob/master/docs/task_init.md#creating-custom-templates
I recommend copying one of the existing init templates to: ~/.grunt/tasks/init/webdesign-project.js and modify from there.
I figured out how to achieve the "custom prompt" - in case anybody is interested:
Grunt's grunt.helper('prompt_for', '...') function apparently only takes a predefined set of values in lieu of '...'. Actually this should not be surprising, as there are some pretty unique features for some of those values (e.g. when you've entered testproject as your project's name, "(git://github.com/peter/testproject.git)" will automatically be proposed.
Solution: Take a look at the .js file of the gruntfile template (node_modules/grunt/tasks/init/gruntfile.js) - creating a custom prompt goes like this:
{
name: 'img_path',
message: 'Name the folder where all image files are located',
default: 'img',
// warning: '' couldn't find any use for this optional property
}
instead of
grunt.helper('prompt_for', 'img_path', 'img')

Resources