I have a problem with render tag in twig.
I got a message: "You have requested a non-existent service "http". When i use {% render url("render_menu") %}
render_menu route works fine
help me !
P/S: Sorry about my english
You use a correct way, but with an old version. You should upgrade to fix security issues AND resolve your problem.
A security issue changed the way to use render: http://symfony.com/blog/security-release-symfony-2-0-20-and-2-1-5-released
Documentation has been updated:
http://symfony.com/doc/2.0/book/http_cache.html#using-edge-side-includes
You are using render tag wrong. If you want to render action, that is behind your render_menu - you should pass it in format YourBundle:YourController:YourAction.
For example, if you have menuAction() that is behind route render_menu, then in Twig you should call it like this:
{% render "YourBundle:YourController:menu" %}
Note, that you have to strip out Action word when calling menuAction in render tag.
Related
I have a site where I am trying to allow the site owners to enter an iframe embed code (using the paragraphs module) on a page and then will render this out adding a container to make it always responsive.
I am running into an issue with twig always outputting as text however. It works perfectly if I use a formatted text field and then click "source" and paste the code in from the dashboard, but obviously that is super clunky for the site admin and very prone to errors. I'd like to just have a plain text field instead. Trouble is even when I use {{ content.field_embed[0].value | raw }} it comes out as text. is there anyway around this that I am missing? Would like to not resort to having the user work with the CK editor just for a simple embed
#mhfleming, I was having the exact same issue as you. Using the filter raw was still giving me the escaped iframe, like in <iframe .... I even thought the issue could be related to one of the contrib modules Twig Tweak and Twig Field Value, which the site I'm working on uses, but those were not the culprits.
I was able to get my code working with the following Twig snippet:
{% set embed_code = {
'#type': 'processed_text',
'#text': content.field_embed_code[0]['#context']['value'],
'#format': 'full_html',
} %}
<div class="responsive-embed">
{{ embed_code }}
</div>
Your code looks very similar. Try replacing the field name with field_embed and give this snippet a try.
Had the same issue and came to thread and found Bruno's answer which gave me an idea that worked for me for a plain text field.
Override the field--name.html.twig that has the iframe code in it. Then print the field using this structure (kint is your friend here) {{ item.content['#context']['value']|raw }}
This worked for me and rendered the iframe just fine. Hope it helps someone else.
I am having tourble creating and routing static pages from within the Sylius CMS.
I have had this working once but now just can not get it to work at all. Creating the content for the page works fine but when I try to save a route all I get is errors.
An exception has been thrown during the rendering of a template
("Parameter "id" for route "sylius_backend_static_content_update" must
match ".+" ("" given) to generate a corresponding URL.") in
SyliusWebBundle:Backend/Content/StaticContent:update.html.twig at line
20.
Image of the page I am trying to save
Any help would be appreciated as there doesn't seem to be any documentation on this part of sylius.
May be you can have a look into the link below. I also had some problems with the routes while creating the pages. I had solved it by adding some info into the database. Hope this link helps you.
https://github.com/Sylius/Sylius-Standard/issues/78
Happy Syliusing ^^
I saw someone including a template on his application using the twig template system.
He included his template like this:
{{ include( 'Bundlename:Directory:template.html.twig' ) }}
instead of this:
{% include 'Bundlename:Directory:template.html.twig' %}
At first I thought he was using a custom twig extension, but it actually
works on symfony in general, even though its not documented.
http://twig.sensiolabs.org/doc/tags/include.html
So is there a difference between the two?
why is it not documented?
if the syntax is wrong why is it even working?
It is documented http://twig.sensiolabs.org/doc/functions/include.html.
Twig tag include vs function include
Lets say I have two controllers that work with the same entity, and use the same set of templates. Every template is supposed to generate buttons/forms/links with URLs to actions of controller that generated this template. So basically, the only thing that is different in the templates are the URLs generated.
You can imagine the problem like a admin CRUD controller with a newAction and editAction, which use the same template, but form actions must differ. My case is more complicated than that, though.
I considered following:
Setting the routes from the controller, but it feels like a lot of code that is out of place.
Different set of templates. Seems like anti-DRY and a lot of reused code.
Using template inheritance, where base template is the one with all the HTML, and child templates only contain the links, but it feels really hacky.
Is there a clever approach to solve this problem?
EDIT: My problem is not generating CRUD. This is more of a "best-practice" question.
I encountered this problem while implementing something like a eshop cart with tons of javascript logic bound to it. It appears twice: in the website, and in a iframe, used by some other devices (iPads and stuff). Both carts have to look the same, but because of different logic, the links must lead to different URLs.
What I ended up doing for now is having 2 templates
The one with all the markup, cart.html.twig:
{% set edit_cart_item = path('edit_cart_item') %}
{% set remove_cart_item = path('remove_cart_item') %}
...html...
Edit item
...more html...
The one for usage in iframe, public_cart.html.twig:
{% extends 'MyCartBundle::cart.html.twig' %}
{% set edit_cart_item = path('public_edit_cart_item') %}
{% set remove_cart_item = path('public_remove_cart_item') %}
Does the problem have some better solution in all the fancy OOP principles?
There isn't a 'clever' approach really. Why not just use the specified Doctrine command to generate it for you, and then go from there? http://symfony.com/doc/current/bundles/SensioGeneratorBundle/commands/generate_doctrine_crud.html
php app/console generate:doctrine:crud with various options
That will get you a solid base done in an 'appropriate' manner, and then you can perform your customizations.
EDIT:
After reading your updated post, I would have a look at this: http://symfony.com/doc/current/book/forms.html#changing-the-action-and-method-of-a-form
So, if you'd like, you can handle the logic of which form to display by passing in options to the form, and then setting the target via setAction():
$form = $this->createFormBuilder($task)
->setAction($this->generateUrl('target_route'))
;
By design, Symfony gives you a lot of flexibility in how to do things. A good reference of their best practices for forms can be found here: http://symfony.com/doc/current/best_practices/forms.html
I also think what you're doing is just fine. When I have complex cases for forms I like to create a Twig template just for the form itself, and then include that in my other templates. In that template you can pass the target route to it if you'd like, and then you'd just have one form template.
First of all, forgive me if this is a silly question. I am new to assemble (started using it last week) and StackOverflow.
I'm trying to use the partial "body" twice on a layout, like this:
{{>body}}
{{>body}}
And I get the error message from the title: The partial body could not be found.
My original intention actually was to use that for selective markdown rendering, such as:
{{#is (extname page.src) "md"}}
{{#markdown}}
{{>body}}
{{/markdown}}
{{else}}
{{>body}}
{{/is}}
But in that case I get the same error when the page is not markdown.
Does this sound like a legit issue? Just trying to help, that's all. :-)
(BTW, for the selective markdown issue I just implemented a {{#markdown-if}} helper that does the job, so I don't need help getting that logic to work. I just want to help assemble in case this behavior I found sounds like a real issue.)
This is an interesting use case... We used to register the page content as a body partial, but stopped doing that and instead use Regex to replace the {{>body}} tag with the page content. I think you should open an issue to request that we substitute all the body tags instead of just the first occurrence.
So to answer the question... Assemble does a string.replace and only replaces the first occurrence, so you'll get an error if trying to use {{>body}} twice.
I was able to fix this in 0.4.12 with the initializeEngine option and changing the two {{> body }} references to just {{ body }}
See https://github.com/assemble/assemble/pull/468#issuecomment-38730532