I have a collection passed from a controller action and added to the view from a variable which I can iterate through for all of the articles in a blog. Each article has its own JSONB column stored in a PostgreSQL table.
I am wondering how to iterate through each article in the blog while JSON decoding each piece of data stored in that specific JSONB column for neat display on a Twig template page. Does anyone have any idea about how to do this? Would it be done in the Controller action and stored into a variable or in a Twig template itself? Could it be done both ways? If so, I would be interested to see how it could be done both ways!
It is easy for me to figure out how to do so on each individual article show page since one article is selected and you can json_decode that one column from the table in the controller then pass a variable (for this example I will call it decoded).
To print a data attribute in a Twig template I can do something like this:
{{decoded['title']}}
Or this:
{{decoded['body']}}
So, how can you achieve something similar with an entire blog collection on a blog index page as opposed to one specific article from the blog table on a show page?
If I were trying to do the same thing with a typical textbook collection in a Twig template the variable passed by the controller to the view (Twig template) would be accessed and iterated through each column in the table like this:
{% for article in blog %}
{article.title}
{article.body}
{% endfor %}
So...I can access the newly added JSON column now similarly by adding it to a similar loop structure on a Twig template page and calling:
{article.json_data}
...but my problem is that there is no json_decode filter for Twig template variable output, only json_encode which can be called in a similar iteration loop like this:
{article.json_data | json_encode}
...but I need json_decode not json_encode, thus my question!
Thank you for any and all help offered!
Related
I have a content type post where the entries have a categories attribute, which is an Array of Links (to a category content type).
I want to fetch all posts that have been tagged with a certain category. That is, Post entries where fields.categories[any link sys.id] = MyCategoryId.
I can only find an example only where the reference field is a singleton, not an array.
Would love any help, thanks!
You could specify your query like this:
/spaces/YOURSPACEID/entries?content_type=CONTENTTYPEID&fields.categories.sys.id=SOMEID
Notice that a content type restriction is necessary as we are filtering on a field property.
Also note that this only works because we're filtering on a system property directly accessible from the actual entry returned. If you wanted to filter on another property of the Category content type, for example title or description that is not currently supported.
Here's also a link to the official documentation including examples and explanations for the search api: https://www.contentful.com/developers/docs/references/content-delivery-api/#/reference/search-parameters/query-entries
I am using a Multiple Document Selector field on one of my document types, so the user can select many documents they like.
Now I would like to loop through each of those documents and apply some HTML tags, my current solution applies the same rule to all of those documents, what I need to be able to do is find the second document that has been selected on the multiple document selector and apply a different rule!
Does anyone know how I can find the second element or any single element from the multiple document selector control.
Hope someone can help me.
Thank you
The Multiple Document Selector is not a standard Kentico control so I don't know how the value is stored but I guess it's something like comma-separated list of NodeAliasPaths. E.g.:
/News/News-1, /Products/Phones, /Services
In that case you can use K# macro:
Documents["/Page"].MutipleDocumentField.Split(",")[0]
// Selects the first document in MultipleDocumentField on page with NodeAliasPath == "/Page"
or
CMSContext.CurrentDocument.MutipleDocumentField.Split(",")[1]
// Selects the second document in MultipleDocumentField on the current page
Macros can also use loops and have lots of built-in methods. See the documentation or the following example:
{% foreach (x in Documents["/Home"].SubmitText.Split(",")) {""+x+""} %}
In my case it generates the following HTML:
/Partners
/News
/Services
You can use macros in all web part fields. Try inserting the code e.g. to static text web part.
I have created a custom content type (via the admin UI) that consists mostly of text fields. I know how to position the fields using zones and Placement.info but for simplicity I would like to use a single view template and to just arrange the fields by name instead of having to use Placement.info. Is there a good way to reference the fields by name from the content item in my MVC view?
So for example, I have a template named Content-MyContentType-Detail.cshtml. Instead of the generic
#Display(Model.Content)
I would like to be able to do something like
#Display(...MyField...)
#Display(...MyOtherField...)
Is there a way to explicitly render a field by name that is associated with my content item?
You can use Shape Tracing module to view model properties and how to get access to content parts.
Then in your template you can use something like this:
<h3>#Model.ContentItem.TitlePart.Title - £#Model.ContentItem.Product.Fields[1].Value</h3>
<p>#Model.ContentItem.BodyPart.Text</p>
If you don't just want to access the field's properties, but rather would like its shape rendered in-place, but like it would be when using placement (i.e. using the existing template), then read this article, which describes how to do direct rendering without placement: http://weblogs.asp.net/bleroy/archive/2013/02/13/easy-content-templates-for-orchard-take-2.aspx
I have a symfony2 Project with an Account entity. It has two attributes id and description.
In my Controller i want to enrich my doctrine Objekts with a further field called name. This property will not be persisted and is only used in my twig template. In twig i want to use {{Account.name}}.
How can i assign values to my objects in my controller that can be used in the twig file?
Thanks, greets mike
oh, its actually so easy :-)
i can simply add
$entity->myproperty = "whatever i want";
and then in my twig template i use
{{entity.myname}}
thanks anyway... i did not know that i can assing properties to my objects like this.
I am working on one already created drupal site. In themes folder it has one tpl.php file, Where theme is done to display the content. Now it is as follows
print $content
And all the fields of cck are displayed properly. But I want to access every field like Title, Body etc. I have tried with $content['title'], But nothing is displayed.
Can anyone please tell me how to access different fields in my tpl file. I am using Drupal 6.
Thanks in advance.
try to
<pre>
print_r($content);
</pre>
then you will see what's exactly in the array.
You can use node-[type].tpl.php to see the different fields. There, by default, each CCK field is exposed in a variable named with the field's name, for example if you have a field called field_logo then you have a variable called $field_logo which is an array representing the field's processed values and can be print_r()'d.
More template suggestions are available in Drupal documentation. And more information on using node.tpl.php and its derivatives is available too.