I am currently trying to translate a string in Symfony/Twig inside an include Statement for a twig Template.
This is the non-translated Code is use for it currenty:
{{ include('#BluelineUser/user/_form.html.twig', {
'back_link': path('user_index'),
'title' : 'Edit User: ' ~ user.username,
'button_caption': 'Save'
}) }}
And in my form I output it like this:
{{ form_start(form) }}
<h1>{{ title|trans }}</h1>
{{ form_end(form) }}
Now if I use the include with a diffrent title (without a variable in it) it works fine.
Adding this to my translation file just doesn't pick up the 'Edit User: '
<trans-unit id="edit.name2">
<source>Edit User: </source>
<target>Benutzer bearbeiten: </target>
</trans-unit>
Any idea on how to get they 'Edit User: ' in the title translated? I think it's breaking because of the variable.
So far couldn't find any example on how to get this working in an include statement.
Try to translate it before like this:
{{ include('#BluelineUser/user/_form.html.twig', {
'back_link': path('user_index'),
'title' : ('Edit User: '|trans) ~ user.username,
'button_caption': 'Save'
}) }}
Related
I have twig template and rendering some data in this template, I have two variant
$datatest = "test" . chr(13) . "test"; //nl2br work good
$datatest = "test\ntest";//nl2br dosn't work, have string in template test\ntest
and second I have query builder, with select some field
$qb
->select('
CONCAT(TRIM(s.streetAddress), \'\\n\', s.postal, s.postOffice) as company_address
')
I try
$qb
->select('
CONCAT(TRIM(s.streetAddress), CHAR(13), s.postal, s.postOffice) as company_address
')
but query builder have error, don't find function CHAR(13), how to create custom DQL for CHAR(13) ?
and in template when dump(data) have string but nl2br not work
I try another filter twig, like this
<div style="font-size: 8px;">
{{ data.company_name|upper }}<br>
{% set address = data.company_address|nl2br %}
{{ address|upper|raw }}
</div>
<br>
<div style="font-size: 9px;">
{{ datatest|nl2br }}
</div>
what need to do in select for nl2br work fine??
You shouldn't format the output inside the SQL query. You're using an MVC framework, so you should definitely stay within it's concepts - let the presentation be done in the view (a.k.a. template).
I recommend select and present tha data this way:
Model:
$qb->select('s.streetAddress, s.postal, s.postOffice')->from ...
Template:
{{ streetAddress }}<br>
{{ postal }} {{ postOffice }}
Trying to render a dropdown in Symfony 2.7.0 but I am having some issues when rendering the choices the view.
$form = $this->createFormBuilder(null)
->add('timespan', 'choice', array(
'choices' => array(90 => "3 months", 30 => "1 month")
))
->getForm();
...
return array(
'form' => $form->createView(),
);
...
Doing var_dump after this will display the values:
var_dump($form->get('timespan')->getConfig()->getOption('choices'));
But when rendering it in the view like this:
{{ form_widget(form.timespan, {'class': 'span2'}) }}
The select box becomes empty.
<select id="form_timespan" name="form[timespan]" required="required" class="span2"></select>
Any ideas why this might occur? Am I missing something?
The problem is obviously in Twig. You can debug this by editing the form theme, to see what values comes in and what is expected. The theme can be found at:
vendor/symfony/symfony/src/Symfony/Bridge/Twig/Resources/views/Form/bootstrap_3_layout.html.twig
(Or you can configure Symfony to use your own.)
You're looking for {% block choice_widget %} specifically to render this.
In this case it looks like you've forgotten to use the attr key for your HTML class:
{{ form_widget(form.timespan, {'attr': {'class': 'span2'}}) }}
You forgot the attr key in your call :
{{ form_widget(form.timespan, {'attr': {'class': 'span2'}}) }}
My Laravel app allows users to modify email templates, then save them to database.
Sample:
Customer {{ $customer_name }} has accepted your proposed timing {{ $timing }} for PO {{ $po_no }}
(User can change 'customer' to 'client' etc.. but leave the {{ }} intact )
The app then loads dynamic data below into above template.
$data = array('customer_email'=> "email#mail.com",
'timing'=> "2pm",'po_no' => "PO001"
);
Then email those rendered text to some emails.
How can I do that?
Rephrase:
load text from db
make it a Blade template
render that template with data
I get stuck at #2
add email view to your views/emails folder
ex: mail_template.blade.php
Customer {{ $customer_name }} has accepted your proposed
timing {{ $timing }} for PO {{ $po_no }}
send method
Mail::send('emails.mail_template', array('customer_email'=> "email#mail.com", 'timing'=> "2pm",'po_no' => "PO001" ), function($message)
{
$message->to('sendto#gmail.com')->subject('your email subject');
});
further read Official Doc
Below is my code for a Laravel 4 project.
Going to the authors/create URL and submitting the form gives me a 405 error.
However, if I prepend the routes.php file with Route::post('authors/store', 'AuthorsController#store');, basically doubling what it already should do, everything works like a charm!
Why do I need do prepend said line in my code to work? I can only assume I'm doing something wrong here.
routes.php:
Route::resource('authors', 'AuthorsController');
AuthorsController.php:
public function create() {
$view = View::make('authors.create');
return $view;
}
public function store() {
//
}
authors/create.twig:
{{ form_open({'url':'authors/store'},{"method" : "post"}) }}
<p>
{{ form_label("Name", "name") }}
{{ form_text("name") }}
</p>
<p>
{{ form_submit("Add Author") }}
</p>
{{ form_close() }}
The store action get's trigger when you POST to the resource. So just authors and not authors/store:
{{ form_open({'url':'authors'},{"method" : "post"}) }}
See this table on more information what URL corresponds to what controller action.
Also I think it should be like this:
{{ form_open({'url':'authors', 'method' : 'post'}) }}
And you can pass the route name Laravel automatically generates to make your life a bit easier:
{{ form_open({'route':'authors.store', 'method' : 'post'}) }}
Oh and one more, post is the default method so this should do as well:
{{ form_open({'route':'authors.store'}) }}
I am trying to use Twig with CakePHP, so I installed this plugin:
https://github.com/predominant/TwigView
And in CakePHP's demo, we build a blog, and I can use this in a tpl file:
{% for post in posts %}
<tr>
<td>{{post.Post.id}}</td>
<td>{{post.Post.title}}</td>
<td>Edit | Delete</td>
<td>{{post.Post.created|date("F j, Y")}}</td>
</tr>
{% endfor %}
What I can't get to work, is converting this:
<?php
echo $this->Html->link(
'Add Post', array('controller' => 'posts', 'action' => 'add')
);
?>
I have tried all of these, and none of them work:
{{ html.link("Add Post", {"controller" : "posts", "action" : "add"}) }}
{{ _view.html.link("Add Post", {"controller" : "posts", "action" : "add"}) }}
{{ this.html.link("Add Post", {"controller" : "posts", "action" : "add"}) }}
I don't get any errors, it just gets replaced with nothing. Anyone know how I can fix this issue?
You have to explicitly declare the helpers in the controller to make it work:
public $helpers = array('Html', 'Form');
See GitHub issue #14 and #13 where I got this from.
Maybe it just won't take an array as the argument or doesn't understand what controller or action is. Try:
{{ html.link("Add Post", "/posts/add" }}
Is it escaping the output? If so, to get the full HTML, use RAW
{{ html.link("Add Post", {"controller" : "posts", "action" : "add"})|raw }}
Proper syntax for hyperlinks html helper is:
{{ html.link('Add Post', '/posts/add') }}