Twig: url encoded twice while passing it to Twitter share button - php

I'm developing a simple page with Symfony2, using Twig as template engine.
I have a list of urls, and I'd like to add the Twitter share button for each url. What I do is a simple cycle on the urls array, and the dinaycally set the url for every Twitter button inside the cycle. It looks like that twig encodes the url at first, and the Twitter script encodes it again. So The Twitter share count doesn't match. The code (inside the cycle) is the following, there is another part of Twitter code at the end of the page:
Tweet
The url I get on the rendered page is: http%253A%252F%252Fwww.example.com%252F (two encoding pass)
instead of http%3A%2F%2Fwww.example.com%2F (one encoding pass, correct). It looks like the % is encoded again to %25.
And this doesn't make Twitter count work, because it consider those two as different urls.
I also tried to use some filters, e.g. {{ s.url|raw }}, but it didn't work.
So my question is: how to avoid this? Is there a way to tell twig (or twitter) to not encode the url?

You can always turn autoescaping off in Twig by using the {% autoescape false %} declaration before the code you want to leave raw. This will leave any strings you output unescaped, and thus your URL will not be escaped twice. Make sure you turn autoescaping back on with {% endautoescape %}
{% autoescape false %}
Tweet
{% endautoescape %}
Full Twig HTML Escaping Documentation

An old post but looks like you can use the "raw" filter now. This should do:
{{ s.url|raw }}
http://twig.sensiolabs.org/doc/api.html#escaper-extension

Related

How do I assign a variable to the value of another variable in TWIG

I am using a PHP product called Mautic which leverages the TWIG template language.
They have some tokens that are accessed similar to
{subject}
and
{contactfield=company}
I want to be able to use the | raw function but when I try
{contactfield=company | raw}
it breaks on me.
If I set a variable then I can use the double curly braces and the functions like raw.
So I am thinking that I need to set a variable to the contactfield=company and then I can access it via the double curly braces. This is how I tried to do it but no luck.
{% set myvar = contactfield=company %}
I tried to use the dump() to guess at what the variable name might be in the context but it appears that command is disabled.
I'm not sure what the syntax should be.
UPDATE
I ran the following code
<ol>
{% for key, value in _context %}
<li>{{ key }}</li>
{% endfor %}
</ol>
and got this result
isNew
slots
content
email
template
basePath
app
cfos
_parent
I suspect that the {contactfield=company} is probably some shorthand for an attribute on one of these variables but not sure how I can dig into the top level context variables to see what other data may be lurking underneath.
I tried using {{dump()}} in the template but that seems to be disabled.
If I knew how to inspect these top level variables I might be able to figure out where this information lives. Unfortunately I don't have access to the PHP environment so I have to discover the information through trial and error.

Find Symfony Widget Content File Location

For the first time, i am working on Symfony widgets.
{% render "EducateToolsBundle:Shared:selectMenu" with {'entity': 'Stores'} %}
its Meaning is SharedController -->selectMenuAction
{% form_theme form 'EducateToolsBundle:shared:_form_theme.html.twig' %}
What is the meaning of this.
From where i am getting the values to this form .?
You must look at selectMenuAction() method in the Shared controller. It is that function that set what template is used. if using the default Symfony coding standards it should be something like selectMenu.html.twig in a Shared sub folder in the Resources/views of the bundle.
You should looks at the documentation first :
https://symfony.com/doc/current/form/form_customization.html
Because we won't teach you symfony here, it's very well explain in the documentation and it is a task too big for us.
In your case theForm.id is the field of the formType we want to displays.
If we want to display the <input> of an form of user lastname we would use
form_widget(userForm.lastname)
form_widget tells symfony to display only the widget (the ) of the id given.
if you want to have a label + input + the errors of the field you could use
form_row(userForm.lastname)
wich is almost equal to
<div>
{{ form_label(form.lastname) }}
{{ form_errors(form.lastname) }}
{{ form_widget(form.lastname) }}
</div>
(it depends of the form theme, but you should read the doc for the details ;) )

Including id element, or variable, from another TWIG template

I have picked up the basics of using TWIG to create my site. I suppose I know how to use {%extend%} {%block%} {%include%} and {%set%}, in the most general sense.
I would like to include a block of code from within another twig file without including the whole file - as opposed to {% include 'file.twig' %}.
I have figured out how to set a variable in file.twig and output it using {{ variable | raw }}. I would like to do that in another file, like you would with using jQuery's .load function.
I swear the twig documentation does not seem to touch on this, it seems like really obvious and basic functionality. I have messed around with various combinations of include, for, with, in and only, colons and commas and whatever | is, and nothing.
I believe you are looking for horizontal inheritance via the use tag:
The use statement tells Twig to import the blocks defined in blocks.html into the current template (it's like macros, but for blocks)
The confusing part is that by itself, {% use ... won't actually insert the content of the blocks in the referenced template. To do that, you must use the block(...) function:
index.twig
{% use "blocks.twig" %}
{{ block('name') }}
blocks.twig
{% block name %}
<h1>Alex Weissman</h1>
{% endblock %}
{% block hobby %}
<p>Blanchin'</p>
{% endblock %}
For a working example, see my TwigFiddle (yes, it's a real thing!): http://twigfiddle.com/jjbfug

how can I var dump a block/node content in drupal 8

I am trying to override block theme and rebuild it with html and twig.
I cant seem to find the variables from the block type or content type and cant find the image url for example.
how can i reach it using kint?
The easiest way to dump everything is with
{{ dump() }}
inside your twig template.
I work on fairly large Drupal sites, and I use this to not exhaust the memory from looping through vars.
<ol>
{% for key, value in _context %}
<li>{{ key }}</li>
{% endfor %}
</ol>
This will dump everything into a nice ordered list.
Hope this helps!
Also I'm not sure if you're already doing this, but if not -- turn on the twig debug tool, then check out your inspector of choice, and it'll give you suggestions and override data.
You can do this inside your sites/default/services.yml with
twig.config:
debug: true
If you have kint (of Devel module) installed, just use:
{{ kint(_context) }}
Its better than {{ dump() }}, cause kint can manage when the recursion is too long, avoiding memory issues. Second, have a nice way to display the information.

Symfony2 simple contact form on every page

I would like to have a simple contact form in the footer of every page. I already created a controller which is working fine on a seperate page. But when i render the controller inside of a template:
{% render(controller('MyBundle:Default:contact', {'request': app.request })) %}
It is working for rendering the form, validation and sending the mail BUT
1.) my flashmessage is not shown:
$this->get('session')->getFlashBag()->add(...)
and 2.) When i try to redirect i get an error
return $this->redirect($this->generateUrl('homepage'))
So my question is:
How can i set the flash message and redirect?
Is rendering a controller in twig actually the best practice? Or are there other approaches?
(I also tried to create a twig extension but ran into different other problems like i cant use formbuilder function etc...)
Method get from FlashBag apart from getting the message in addition removes it... So probably you've invoked get method somewhere before. For example following code always shows nothing:
{% if (app.session.flashbag.get('message')) %}
<div class="message">{{ app.session.flashbag.get('message') }}</div>
{% endif %}
If this is the case, then instead of the first get you should use has method in the condition.
Moreove if you want get and only get the message from the flash bag - without removing it, you can use peek method.

Categories