Passing data to universal twig file Symfony2 - php

I want to send user data to my univeral header file, which is shown on every page so I can display a "Hi, {{ username }}" message.
How can I do this without getting the data in all controllers of every page?

You can access current user in your twig template via the global variable app, using app.user :
{# If user is connected #}
{% if is_granted('ROLE_USER') %}
Hi, {{ app.user.username }}
{# If user is NOT connected #}
{% else %}
...
{% endif %}

To get the currently logged in user, you can simply write: {{ app.user }}. Then you can access any property from there.

You can also render data from a totally different controller and action (so just from one place) in your twig template by using:
{{ render(controller('AppBundle:Base:myAction')) }}
You can read it over here: https://reformatcode.com/code/c/calling-r39s-optim-function-from-within-c-using-rcpp

Related

How to translate partial/variable in partial in OctoberCMS with rainlab.translate plug-in?

Just started using OcteberCMS and now trying to figure out how to translate with rainlab.translate plug-in partial (no, I can't use here {{ ''|_ }} for some reason).
For example, code of layout:
{% partial "footer" %}
I have 2 files in "partials" dir:
footer.htm and footer.fr.htm
but always footer.htm including, but not footer.fr.htm when I'm switch language to fr.
Or maybe is there some way to pass translated variable to partial?
{% partial "sidebar-contacts" city="Vancouver" country="Canada" %}
{% partial "sidebar-contacts" city="{{ 'Vancouver'|_ }}" country="Canada" %}
Thanks in advance.
It's very simple: just pipe variable |_ into translate plug-in like this:
{% partial "sidebar-contacts" city="Vancouver" country="Canada" %}
In file sidebar-contacts:
<div class="sidebar-contacts">
{{ city|_ }}, {{ country|_ }}
</div>

Symfony and data input

Currently I am working on a controller class which should be able to let a customer add an item to a table in a database and view these items (e.g. a movie).
I am using the following twig code:
{% extends 'base.html.twig' %}
{% block body %}
{% if movies|length == 0 %}
There are no movie items available. Add a movie here to get started.
{% elseif movies|length != 0 %}
These are the results: ...
{% endif %}
{% endblock %}
What is the best way to let a user/customer or whatever the case, add an item to the table that needs to be shown? Should I let a user fill a form on the exact same page as the overview template (I personally do not think this is good, as I want to keep purpose of each page seperated) or should I make a different template with a form where the user will be send to (though this takes redirection time, which some users might be getting annoyed by)? As you can see I am using an anchor tag on the word "here". How should I set-up the anchor tag if I am to use a different template for creating records in a table?
As the documentation of Symfony shows the following:
Home
The path says _welcome and I think it refers to the name of the route that points to a certain controller. Would it be best to use this path function and what would I need to add where it now says _welcome (or was I correct one sentence ago)? And why is there an underscore in the given example?
I am asking this question because when I worked with ASP.NET MVC, there was this method called ActionLink() and made me wonder if this is most common use of redirecting, since you could also just add the template file location to the anchor tag href attribute?
If the form to add a new item is small (one text field + one submit button), you could add the form in the same page.
For example :
{% extends 'base.html.twig' %}
{% block body %}
// display your form here
{% if movies|length == 0 %}
There are no movie items available.
{% elseif movies|length != 0 %}
These are the results: ...
{% endif %}
{% endblock %}
But actually it's up to you to decide if you want it to be displayed in the same page or not.
In the case you decide to redirect the user to a new template, especially for the form, you write the name of the route of the correspondant controller :
here
So the code of the controller will be executed and will redirect to the page of your form.
Concerning "_welcome", I don't know why they write it like this. This is more the way to name a layout file than a route name.

Wordpress Advanced Custom Field Plugin + Twig template

I'm creating a real estate website with the template Realia. This theme is based on Twig files and here's my problem. I have a front end submission where we can add a custom post ( a property ). I want to add a custom field to this form. The code which get and display the field is made and work because it is in a php file ;
<?php acf_form_head(); ?>
<?php acf_form( array(
'field_groups' => array(1943),
'form' => false,
) ); ?>
But now I want to save the data of my field and the button "Add my property" is in a Twig file..
Here's the button code
<div class="form-actions">
{% set value = wp.__('Save', 'aviators') %}
<input type="submit" class="btn btn-primary" value="{{ value }}">
</div>
{% endif %}
</form>
According to this documentation , the acf code is acf_form_head() but I dunno how to put it in my code. I try {{ acf_form_head() }}, {{ wp.acf_form_head() }} and some other sentence but nothing works... I tried to to find the "save" function which is on this php file but I don't know to edit it..
Please, could someone help me ?
Thank in advance
Jennifer O.
As far as i know wp-realia theme.
{{ }}
These tags are used for outputting some thing to browser or calling methods.
add wp. before calling any function wp. means that this functions is user defined or core function. if you call function without wp. prefix it means you are calling twig template function.
{% %}
These tag are used for calling core twig functions eg {% if my_var %}.
For your scenario you want to call acf_form_head() function in header to print css/js and necessary files in the header so you can make a block in the realia/templates/helpers/header.twig file in head tag eg:
{% block header_block %}{% endblock %}
then in your custom twig template file reference that block and put your content in it:
{# we tell our custom template to extends from layout.twig #}
{% extends 'layout.twig' %}
{# Add acf_form_head() function in our header block #}
{% block header_block %}
{{ wp.acf_form_head() }}
{% endblock %}
{# add this content to our content block which is define in layout.twig file #}
{% block content %}
{% if wp.have_posts() %}
{% for post in posts %}
{{ wp_query.the_post() }}
My custom field: {{ wp.the_field('my_custom_field') }}
{{ wp.acf_form() }}
{% endfor %}
{% endif %}
{% endblock %}
Hope it will help you.

Detecting specific form constraint failure in Twig template

I have a field in a Symfony form which contains three constraints.
For one of the three constraints, if validation fails, I want to trigger a popup notice on the form once the page has reloaded. This looks beyond the scope of Twig's form_errors() function.
Is there any way of finding out if a specific constraint on a single field has failed in my Twig template? I'm struggling to find a way to access this information right now. (I'm not opposed to passing a flag to the template from my controller if necessary.)
(Might be worth adding that this is a Silex project.)
You can check form.field.vars.errors like this:
<input type="text" name="msisdn"
class="{{ form.msisdn.vars.errors|length ? 'error' : '' }}"
value="{{ form.msisdn.vars.value }}" />
The actual errors are in an array, you can loop and output them individually like this:
<span class="errorMessage">
{% for error in form.msisdn.vars.errors %}
{{ error.message }}{% if not loop.last %},{% endif %}
{% endfor %}
</span>

phalcon php : get element at index

How do we get an element at index i in *.volt view?
I know this for loop
{% for robot in robots %}
{% if robot.type == "cyborg" %}
{{ robot.name }}
{% endif %}
{% endfor %}
but I would like to print the name of robot at index 5 only, and I don't care about other robot names.
Can I access robot at index 5 without using for loop?
Volt templates are compiled to PHP code (you can manually check it in the *.volt.php files), so you can use similar syntax to access array keys in the loop:
{% for key, robot in robots %}
{% if key == 5 %}
{{ robot.name }}
{% endif %}
{% endfor %}
or you can use regular PHP syntax to access element by index without loop:
{{ robots[5] }}
Also, looks like there is a bug with object in array case, so you can use PHP code in the Volt template to resolve your issue without loop:
<?php echo $robots[5]->name ?>
Volt is indeed based on twig but there are some functions which aren't implemented, so you should do better using the original volt documentation: http://docs.phalconphp.com/en/latest/reference/volt.html#variables
Anyways the answer is almost correct, you can access arrays via index but keep in mind that the array index begins with "0" so the correct answer is:
{{ robots[4] }}
You should be able to access a particular index of an array like this:
{{ robots[5] }}

Categories