How to display the content.field_xxx in twig (Drupal 7) - drupal-7

I have added a custom field field_instruction (plain text field) in a new content type.
I would like to display the field like {{ content.field_instruction }} in node.html.twig. However, it failed.
I tried {{ content.field_instruction[0] }} or {{ content.field_instruction.value }}, it does not success.
Can anyone advise me how to display this field?

To render a field's default value inside node.html.twig, try use a Twig variable as in {{ node.field_some_name.value }}.

Related

AngularJS to display one section in Italic

Hi i'm creating a Harvard Reference Generator using AngularJS ive got it working perfectly, i can get it to create the full reference however i need one section to be styled in italics, the information is captured using a form. I require a little help with the output.
Current Output Code:
{{
book_author+" "+book_multiple_authors+" ("+book_year+").
"+book_title+".
"+book_place+":
"+book_publisher+".
"+ book_edition
}}.
What i need is for one section "book_title" to be shown in italics
I have tried:
{{
book_author+" "+book_multiple_authors+" ("+book_year+").
<i>"+book_title+".</i>
"+book_place+":
"+book_publisher+".
"+ book_edition
}}.
{{
book_author+" "+book_multiple_authors+" ("+book_year+").
"+<i>book_title</i>+".
"+book_place+":
"+book_publisher+".
"+ book_edition
}}.
{{
book_author+" "+book_multiple_authors+" ("+book_year+").
"<i>+book_title+</i>".
"+book_place+":
"+book_publisher+".
"+ book_edition
}}.
Nothing seems to work, i cant find any further reference to formatting the angularjs output specific to one section. What am i missing, any help would be greatly appreciated. That You.
To have HTML rendered within ng expressions, you need to use ngSanitize or $sce dependency. Else, HTML will be rendered as string inside your expressions.
In your case, you can simplify it by breaking it down to multiple elements:
<span ng-bind="book_author"></span>
<span ng-bind="book_multiple_authors"></span>
<span ng-bind="book_year"></span>
<i>
<span ng-bind="book_title"></span>
</i>
<span ng-bind="book_place"></span>
<span ng-bind="book_publisher"></span>
<span ng-bind="book_edition"></span>
Code between double curly braces in AngularJS is meant to be JavaScript, so it can't contain HTML tags. The way around it would be along these lines:
{{book_author+" "+book_multiple_authors
+" ("+book_year+").&nbsp";}}
<i>{{book_title}}</i>
{{" "+book_place+": "+book_publisher
+". "+book_edition;}}
Let me know if this works for you, if not we can try CSS instead to render text in italics.
Best wishes,
Lukasz

How to pass entire objects from Controller to Twig in Symfony 3?

I'm triyng to pass an object to Twig.
The object is the representation of an Entity, obtained via the
getDoctrine()->getManager()->getRepository(/*repoName*/)->find(id);
This actually works but how can I display all of its values in a html table in Twig?
I tried serialization but with no success, maybe I am missing something, please help.
Thanks in advance!
UPDATE:
What I actually want to achieve is to iterate to that object WITHOUT knowing its keys, a sort of
foreach (field in object) print key, value
$object = $em->getDoctrine()->getManager()->getRepository(/*repoName*/)->find(id);
You need pass this variable to the template by :
return $this->render('Anypath/your_template.html.twig', ['obj'=>$object]);
than from the twig :
{{obj.id}} or {{obj.name}}
depends on your fields inside object.
In your controller:
return $this->render('path/template.html.twig', ['entity'=>$entity]);
and in your template (replace your_attribute_name by any attribute of your entity):
{{ entity.your_attribute_name }}
Once you send your object to you twig template
return $this->render("AppBundle:Records:template.html.twig", [
"$object" => $object
]);
You can just do:
{{ object.field }}
That correspond to doing $object->getField() in PHP
Then just build your list manually in your twig
You you wanna loop through your object have a look at thise subject
Twig iterate over object properties
{% for key, value in my_object|cast_to_array %}
This might help

Showing dynamic value in translated string

Im using Angular Translate, specifically the filter translate with dynamic values (https://angular-translate.github.io/docs/#/api/pascalprecht.translate.filter:translate). I want to translate a string and include a dyncamic value. In my translations file I have:
products:
searchResults: "Search results for {{ searchTerm }}"
And in my jade template:
.bg-info(ng-show='productVM.search') {{ 'products.searchResults' | translate:'{searchTerm: productVM.search}' }}
So when I type, the controller property search contains the string and the div shows, except the result is:
Search results for
If I was to replace 'productVM.search' with 'test', it would work:
Search results for test
It appears that angular translate doesnt like if the value pass to it is dynamic. Anyway around this?

How to make the text display the same in a ng-model from a select with ng-options?

I am new to angular (problem one). I am trying to tie the formatted text from a select with ng-options to the ng-model that is being called in the page. What happens is the below take language codes like en-US and changes the text to English, which displays in the select perfectly. When I use {{ textSelect }} it displays as en-US. So how do I get it to display correctly?
<select ng-model="textSelect" ng-options="language.code as language.name for language in availableLanguages"></select>
<p>{{ textSelect }}</p>
<select ng-model="textSelect"
ng-options="language as language.name for language in availableLanguages"></select>
<p>{{ textSelect.name }}</p>
Is one suggestion.

jinja2 form render does not allow attribute which contains "-"

I am trying to customize the form template base on this tutorial. As I understand, render() just add some attributes to the tag. For example, I add placeholder = "abc" and it works well.
{% call inserttourbus(id = "formAddNewRow" ) %}
<div class="fieldWrapper">
{% if inserttourbus['bustype'].label() %}Bus Type{% endif %}
{{ inserttourbus['bustype'].render(placeholder="abc")|safe }}
{% if inserttourbus['bustype'].errors() %}Not filled yet!{% endif %}
</div>
{% endcall %}
Here is my problem:
- I use bootstrap typeahead for my template so I need to add the following attribute to the inserttourbus textbox
data-provide="typeahead" data-items="4" data-source='["Alabama","Alaska"]'
So it will become
{{ inserttourbus['bustype'].render(placeholder="abc", data-provide="typeahead", data-items="4", data-source='["Alabama","Alaska"]')|safe }}
But the jinja2 engine seems does not accept data-provide, data-items, so on because it contain "-" character. If I changed data-provide to dataprovide, the jinja2 engine can render the code well.
However, in bootstrap typeahead javascript, all variables are defined as data-provide, data-items. If I change them to dataprovide, dataitems, the javascipt stop working.
Please give me a solution:
- How to make jinja2 accept attribute which has "-"
- Other solutions, advices
Check out this snippet for doing this in Flask. I imagine it would work the same way for Django; pass HTML attributes with invalid Jinja2 (Python) syntax inside an ad-hoc dictionary:
{{ inserttourbus['bustype'].render(placeholder="abc",
**{'data-provide':'typeahead',
'data-items':'4',
'data-source':'["Alabama","Alaska"]'}) }}
A Hyphen is used as the subtraction operator in Python. So do not use it in names. You can use it ofcourse in strings.

Resources