What I trying to do is to echo the title if a custom field is empty.
{{ if custom_fields:alternative_title != "" }}
{{ custom_fields }}{{ alternative_title }}{{ /custom_fields }}
{{ else }}
{{ title }}
{{ endif }}
This code didn't work.
Thanks
Related
I have a Laravel collection with relations returned to a view and I need to access it's properties;
controller
$project = Project::with('pulls.builds')->where('id',$id)->get();
view
#foreach ($project as $project->pulls)
{{ $project->pulls }}
#endforeach
output
{"id":2,"created_at":null,"updated_at":null,"name":"Project Name","pulls":[{"id":2,"created_at":null,"updated_at":null,"branch":".fixes","project_id":2,"builds":[{"id":3,"created_at":null,"updated_at":null,"description":"43662-4768-456","pull_id":2}]},{"id":3,"created_at":null,"updated_at":null,"branch":".test","project_id":2,"builds":[{"id":4,"created_at":null,"updated_at":null,"description":"build 1","pull_id":3}]}]}
I have all the data that I want, now I need to make it look like this when viewed by the user:
Project Name
.fixes
43662-4768-456
.test
build 1
$projects = Project::with('pulls.builds')->where('id',$id)->get();
Given the structure of the json, to echo out every property you'd need to do something like this
#foreach($projects as $project)
{{ $project->id }}
{{ $project->created_at }}
{{ $project->updated_at }}
{{ $project->name }}
#foreach($project->pulls as $pull)
{{ $pull->id }}
{{ $pull->created_at }}
{{ $pull->updated_at }}
{{ $pull->branch }}
{{ $pull->project_id }}
#foreach($pull->builds as $build)
{{ $build->id }}
{{ $build->created_at }}
{{ $build->updated_at }}
{{ $build->description }}
{{ $build->pull_id }}
#endforeach
#endforeach
#endforeach
So taking only what you need, and adding some html, it could end up looking like this:
<p>Projects:</p>
#foreach($projects as $project)
<p>Project Name: <span>{{ $project->name }}</span></p>
<p>Pulls:</p>
#foreach($project->pulls as $pull)
<p>Branch: <span>{{ $pull->branch }}</span></p>
<p>Builds:</p>
#foreach($pull->builds as $build)
<p>Description: <span>{{ $build->description }}</span></p>
#endforeach
#endforeach
#endforeach
If you only want a single project, instead of using ->where('id', $id)->get(), use ->find($id) and you can remove the first #foreach.
First, I have found this question:
How can I set the default value for an HTML <select> element in Golang?
But in my case I don't know how to do.
I have many posts data and render them to template.
Controller:
c.Data["posts"] = posts_data
View:
{{ range $post := .posts }}
<option value="{{ $post.ID }}">{{ $post.Name }}</option>
{{ end }}
Works fine.
But if change to:
Controller:
c.Data["posts"] = posts_data
c.Data["post_id"] = param_data
View:
{{ range $post := .posts }}
<option value="{{ $post.ID }}" {{ if eq $post.ID .post_id }}selected="selected"{{ end }}>{{ $post.Name }}</option>
{{ end }}
Got error:
template Execute err: template: posts/index.tpl:20:70: executing "posts/index.tpl" at <.post_id>: can't evaluate field post_id in type models.Post
It's true that post_id doesn't exists in models.Post. But how to use it in this way?
The range command sets . to the current value. Use $ to refer to the root value passed to the template:
{{ range $post := .posts }}
<option value="{{ $post.ID }}" {{ if eq $post.ID $.post_id }}selected="selected"{{ end }}>{{ $post.Name }}</option>
{{ end }}
Because range sets ., the template can be simplified to:
{{ range $post := .posts }}
<option value="{{ .ID }}" {{ if eq .ID $.post_id }}selected="selected"{{ end }}>{{ .Name }}</option>
{{ end }}
I want to show metadata from the markdown file on the webpage so I'm trying to access it using the variable names (e.g. {{ .Author}} ).
This works fine with the .Title and .Content variables, but does not work with the others! It seems like I'm missing an important detail on how to use these. With the .Author variable the output on the page is { map[]}.
Thanks in advance
Markdown file:
---
title: ABC
author: "Foo Bar"
position: Manager
---
The actual content ...
Webpage:
{{ range where .Data.Pages "Type" "type"}}
<section>
<div>
<div>
{{ .Title }}<br>
{{ .Content }}
</div>
<div>
{{ .Author }}<br>
{{ .Position }}
</div>
</div>
</section>
{{ end }}
Turns out you need to access the non-standard parameters via the .Params variable.
See https://gohugo.io/variables/page/ for the relevant information.
{{ range where .Data.Pages "Type" "type"}}
<section>
<div>
<div>
{{ .Title }}<br>
{{ .Content }}
</div>
<div>
{{ .Params.author }}<br>
{{ .Params.position }}
</div>
</div>
</section>
{{ end }}
If you are building a menu, it seems .Params.author fails. So this should work:
Let’s say you have in your .md file the metadata:
author: Bob Dole
menu: "guest"
Your menu can access the author if you use {{ with .Page }} and {{ .Params.author }}
{{ range .Site.Menus.guest }}
<div>
{{ with .Page }}
{{ .Name }} - {{ .Params.author}}
{{ end }}
</div>
{{ end }}
In Jekyll, a post is displayed in its layout thusly:
{{ content }}
Any generators that might have run on that content is displayed like so:
{{ content | toc_generate }}
Unfortunately, this does not work on index pages, as {{ content }} displays nothing on index pages. Instead, we are told to use a for loop:
{% for post in site.posts %}
{{ post.content }}
{% endfor %}
So, the question:
How can I get a generator to run on an index page, since I can't use {{ content }}?
My best guess...
{% for post in site.posts %}
{{ post | toc_generator }}
{% endfor %}
...does nothing.
Any help?
Try this :
{% capture content %}
{% for post in site.posts %}
<h2>{{ post.tile }}</h2>
{{ post.content }}
{% endfor %}
{% endcapture %}
{{ content | toc_generator }}
{{ content }}
I want to render a form. HTML for a field row should be like this:
<li class="text">
<label for="fieldname">
<div>
<input type="text" ... />
</div>
</li>
when the field type is text the li.class have to be the same.
I overwrite the field_row block:
{% block field_row %}
{% spaceless %}
<li class="text">
{{ form_label(form, label|default(null)) }}
{{ form_errors(form) }}
{{ form_widget(form) }}
</li>
{% endspaceless %}
{% endblock field_row %}
but how to replace the class value?
You can try to attach a public member to your FormType class (If present...) and call it from the twig template.
Maybe also the attributes array of a form can be accessed in a twig template...
class YourType extends AbstractType
{
public $class = 'text';
public function buildForm(FormBuilder $builder, array $options)
{
$builder->add('fieldname');
}
//...
}
And
{% block field_row %}
{% spaceless %}
<li class="{{ form.class }}">
{{ form_label(form, label|default(null)) }}
{{ form_errors(form) }}
{{ form_widget(form) }}
</li>
{% endspaceless %}
{% endblock field_row %}
Just replace the "field" word with the name of the type you want to modify.
You do it like this for text fields, but it's the same for any type:
{% block text_row %}
{% spaceless %}
<li class="text">
{{ form_label(form, label|default(null)) }}
{{ form_errors(form) }}
{{ form_widget(form) }}
</li>
{% endspaceless %}
{% endblock text_row %}
or like this for textareas:
{% block textarea_row %}
{% spaceless %}
<li class="textarea">
{{ form_label(form, label|default(null)) }}
{{ form_errors(form) }}
{{ form_widget(form) }}
</li>
{% endspaceless %}
{% endblock textarea_row %}
The important part is the block name, it should be the same as the name of the type you want to modify. The "field_row" is the default for all field types if there is no exact matching name.
This also works for form-types you defined yourself (the ones that inherit from AbstractType, that's why it's important you add a name to your form-types, see http://symfony.com/doc/2.0/book/forms.html#creating-form-classes).