HearsayRequireJSBundle: Javascript files not found - symfony

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' }) }}

Related

How to set up default manifest_json_path while having multiple configurations?

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

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

Unable to find template Bundle:Default:index.html.twig

I'm having some trouble runnning Symfony. In fact, it can't find the default twig template. I didn't touch the code at all, juste generated my bundle and trying to access /web/app_dev.php.
My template is in
/src/OC/PlatformBundle/Resources/views/Default/index.html.twig.
Symfony looked into these locations, but not where my template actually is.
/app/Resources/views
/vendor/symfony/symfony/src/Symfony/Bridge/Twig/Resources/views/Form
And the DefaultController.php indexAction() looks ok :
public function indexAction(){
return $this->render("OCPlatform:Default:index.html.twig");
}
If any of you have ever faced this kind of issue or have any idea where the problem comes from, I thank you in advance for your help.
Arthur
I've same problem and i resolve it with this route :
public function indexAction(){
return $this->render("#OCPlatform/Default/index.html.twig");
}
Edit ":" with "/" and it work.
Maybe can help other developper
If you want to keep using the 'OCPlatform:Default:index.html.twig' format for templates then edit your app/config/config.yml file:
#app/config/config.yml
framework:
templating:
engines: ['twig']
This fixed my problem. You can check this link
Symfony 3.4 Use view inside my bundle
Following the docs(Symfony >=3.4) you should write the path to the template in the controller like this:
public function indexAction(){
return $this->render("#OCPlatform:Default:index.html.twig");
}
This worked for me:
return $this->render('#HomeBundle/Default/index.html.twig');
Hope it hope :)
Referencing Templates in a Bundle¶
If you need to refer to a template that lives in a bundle, Symfony uses the Twig namespaced syntax (#BundleName/directory/filename.html.twig). This allows for several types of templates, each which lives in a specific location:
#AcmeBlog/Blog/index.html.twig: <br>This syntax is used to specify a template for a specific page. The three parts of the string, each separated by a slash (/), mean the following:<br>
#AcmeBlog: is the bundle name without the Bundle suffix. This template lives in the AcmeBlogBundle (e.g. src/Acme/BlogBundle);<br>
Blog: (directory) indicates that the template lives inside the Blog subdirectory of Resources/views/;<br>
index.html.twig: (filename) the actual name of the file is index.html.twig.
In symfony 4 use this :
return $this->render("#Platform/Default/index.html.twig");

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

Symfony How to doing translate jordillonch / CrudGeneratorBundle

I have in my template something like that
<h1>Workers {{ 'views.index.list'|trans({}, 'JordiLlonchCrudGeneratorBundle') }}</h1>
And I want to translate this but I don't know how. I guess have to create a file with translations but where save it ,how name a file and what write inside?
In app/config/config.yml I have 'translator: { fallback: "%locale%" }'
In 'symfony/app/config/parameters.yml' defined locale parameter "locale:pl"
First look at the bundle's Resources / translations folder. It looks loke this:
JordiLlonchCrudGeneratorBundle.ca.yml
JordiLlonchCrudGeneratorBundle.en.yml
JordiLlonchCrudGeneratorBundle.es.yml
Then create a file JordiLlonchCrudGeneratorBundle.pl.yml (if you want to translate to the pl local) and put it to the app/Resources/JordiLlonchCrudGeneratorBundle/translations directory.
Use one of the default translation files as a pattern for your translation, translate all text you need and save it to the pl translation file you've just created.
See also: http://symfony.com/doc/2.1/cookbook/bundles/inheritance.html#overriding-resources-templates-routing-validation-etc

Resources