I am trying to include source code of a c program in my jekyll pages with download link.
I have searched jekyll documentation and also stackoverflow but nothing is working.
Can anyone point out the right way to do this.
---
source: ["inf.c","find.c","error.c","stu.c","info.c"]
---
{% for c in page.source %}
<tr>
<th>
<a href="{{c}}">
{{c}}
</a>
</th>
</tr>
<tr>
<th>
{% highlight ruby %}
{% include {{page.permalink}}{{c}} %}
{% endhighlight %}
</th>
</tr>
{% endfor %}
Let's say that your code snippets a re stored in /code folder as a.c, b.c and so on.
Your page code.html can be like this one :
{% for c in page.source %}
{% capture filePath %}/code/{{c}}{% endcapture %}
{{c}}
{% highlight c %}
{% include_relative {{ filePath }} %}
{% endhighlight %}
{% endfor %}
Related
I am trying to access a variable of a object in jinja2 using this code:
{% for row in rows.items %}
<tr>
{% for column in model_settings['table_columns'] %}
<td>
<p>{{ row.column }}</p>
</td>
{% endfor %}
</tr>
{% endfor %}
what I want is if column was username I would want it to give me
row.username. sorry for the bad explanation.
Jinja2 has the builtin attr filter to access an attribute of an object. The name of the attribute can be a static string, or in your case: a string stored in a variable:
{% for row in rows.items %}
<tr>
{% for column in model_settings['table_columns'] %}
<td>
<p>{{ row|attr(column) }}</p>
</td>
{% endfor %}
</tr>
{% endfor %}
I am new to craft and twig and am trying to access table values inside of a neo matrix and started running into some blockers. I don't know how to query the values properly I think. Here is my code:
{% if row.tableColumn|length %}
<table>
{% set policyTable = row.tableColumn.all() %}
{{ dump(row.tableColumn.all()) }}
{% for row in policyTable %}
<thead>
<tr>
<th>
{{ row.col1 }}
</th>
</tr>
<tr>
<td>
{{ row.column1 }}
</td>
</tr>
</thead>
{% endfor %}
</table>
{% endif %}
And here is the dump for reference (shortened):
array(1) { [0]=> object(craft\elements\MatrixBlock)#2695 (67)
{["genericRichTextOnlyListItems"]=> NULL ["tableColumn"]=> string(402)
"[{"col1":"THEAD1","col2":"THEAD2","col3":"THEAD3","col4":"THEAD4"},{"col1":"ROW1","col2":"60-75","col3":"15-30","col4":"5-20"},{"col1":"ROW2","col2":"45-55","col3":"35-45","col4":"5-20"},{"col1":"ROW3","col2":"5-15","col3":"75-85","col4":"15-25"}]"
} }
I am trying to access col1 in tableColumn which I have named column1 on the craft backend. {{ row.col1 }} and {{ row.column1 }} will not work. Any tips?
Looks like policyTable is an array of Matrix Blocks. Each array item (though there's only one) has the table columns under the key tableColumn, so you can try something like this (I renamed policyTable to policyTables since it's an array):
{% if row.tableColumn|length %}
<table>
{% set policyTables = row.tableColumn.all() %}
{% for row in policyTables[0].tableColumn %}
<thead>
<tr>
<th>
{{ row.col1 }}
</th>
</tr>
<tr>
<td>
{{ row.col2 }}
</td>
</tr>
<!-- etc. -->
</thead>
{% endfor %}
</table>
{% endif %}
Or if it's possible that there's more than one matrix block, you could try something like this:
{% if row.tableColumn|length %}
{% set policyTables = row.tableColumn.all() %}
{% for table in policyTables %}
<table>
{% for row in table.tableColumn %}
<thead>
<tr>
<th>
{{ row.col1 }}
</th>
</tr>
<tr>
<td>
{{ row.col2 }}
</td>
</tr>
<!-- etc. -->
</thead>
{% endfor %}
</table>
{% endfor %}
{% endif %}
In both cases, you probably want to handle the first row differently since it seems to be the heading row. You can utilize Twig's loop variable for this. (There might be some other ways as well.)
I'm trying to modify Flask-Security's _macros.html to nicely align everything. I replaced the p tags with tr and wrapped {{ field.label }} and {{ field(**kwargs)|safe }} both between td tags. For some reason those tags totally vanish from the output poof, like they've never been there. Using some nonsense like trrrrr and tdddd outputs just fine, so I know I'm editing the right file.
What's going on? Why does jinja2 hate my tables?
I also tried about wrapping the macro call in the template between <td></td> and then every supposedly wrapping tag just appear after all the macro calls without any content in between (but above).
I tried googling my vanishing tags but couldn't find anything.
{% macro render_field_with_errors(field) %}
<tr>
<td>{{ field.label }}</td> <td>{{ field(**kwargs)|safe }}</td>
{% if field.errors %}
<ul>
{% for error in field.errors %}
<li>{{ error }}</li>
{% endfor %}
</ul>
{% endif %}
</tr>
{% endmacro %}
{% macro render_field(field) %}
<td>{{ field(**kwargs)|safe }}</td>
{% endmacro %}
I was going through the django docs and found this code in the template,
{% if latest_question_list %}
<ul>
{% for question in latest_question_list %}
<li>{{ question.question_text }}</li>
{% endfor %}
</ul>
{% else %}
<p>No polls are available.</p>
{% endif %}
So could someone please explain what the {% %} tag stands for. Is it like <? ?> tag in php and if so why not add a single tag and print the rest of the html code?
I'm trying to custom the add/delete feature of a collection with the data-prototype attr.
To do so, I use some twig macro:
view called by the controller
{# the principal view .html.twig #}
{% from"AppBundle:Macros:tabRangeVal.html.twig" import tableau %}
[...]
{{ tableau('InstrumentSharedUtils/File',form.documentations) }}
{{ tableau('SpecificationsSheet/SpecificationSheet',form.list) }}
[...]
macro for tableau
{# /src/AppBundle/Resources/views/Macros/tabRangeVal.html.twig #}
{% macro tableau(classe, arrayVar) %}
{% from "AppBundle:Form:"~ classe ~ ".html.twig" import val %}
{% from "AppBundle:Form:"~ classe ~ ".html.twig" import label %}
{% from "AppBundle:Macros:dataProto.html.twig" import proDataType %}
<div class="well well-sm">
<h5> {{ form_label(arrayVar) }}</h5>
<table class="table">
<thead>
<tr>
<th><button class="btn btn-default btn-add-row" type="button" id="{{ "add_#{arrayVar.vars.id}" }}"><span class="glyphicon glyphicon-plus"></span></button></th>
{{ label() }}
</tr>
</thead>
<tbody id="{{ "container_#{arrayVar.vars.id}" }}" data-prototype="{{ proDataType(classe, arrayVar.vars.prototype)|e }}">
{% for i in arrayVar %}
<tr >
<td><button class="btn btn-default btn-delete-row"><span class="glyphicon glyphicon-minus"></span></button></td>
{{ val(i) }}
</tr>
{% endfor %}
</tbody>
</table>
</div>
{% endmacro %}
macro for the data-prototype field
{# /src/AppBundle/Resources/views/Macros/dataProto.html.twig #}
{% macro proDataType(classe, value) %}
{% from "AppBundle:Form:"~ classe ~ ".html.twig" import val %}
<tr>
{% for v in value %}
<td><button class="btn btn-default btn-delete-row"><span class="glyphicon glyphicon-minus"></span></button></td>
{{ val(v) }}
{% endfor %}
</tr>
{% endmacro %}
an exemple of the AppBundle:Form:"~ classe ~ ".html.twig
{# /src/AppBundle/Resources/views/Form/InstrumentSharedUtils/File.html.twig #}
{% macro val(var) %}
<td>{{ form_widget(var.name, { 'attr': { 'class':'form-control' } }) }}</td>
<td>{{ form_widget(var.path, { 'attr': { 'class':'form-control' } }) }}</td>
<td>{{ form_widget(var.isExtra) }}</td>
{% endmacro %}
{% macro label() %}
<th>Name</th>
<th>Path</th>
<th>Is Extra</th>
{% endmacro %}
The problem is that I get the error
Method "name" for object "Symfony\Component\Form\FormView" does not exist in AppBundle:Form:InstrumentSharedUtils/File.html.twig at line 2
The thing that really bothers me is that I can access to the name (and even the others prototype.vars.children) if I dont use macro
by exemple, the following code works:
{# principal view .html.twig #}
{% for i in form.documentations.vars.prototype %}
{{ form_widget(i) }}
{% endfor %}
and i feel like its the exact same as above, with
form.documentations => arrayVar,
arrayVar.vars.prototype => value, and
{% for i in value %}
{{ form_widget(i.name) }}
{{ form_widget(i.path) }}
{{ form_widget(i.isExtra) }}
{% endfor %}
the output should be the same ...
could you help me to find what is wrong in there?
Ok, I will probably flog myself to be so stupid, but by writing it down, I realised (in the last code section) that I made a mistake:
the i in value at the end is already either name or path or isExtra...
so when I try to access to form.documentations.vars.prototype.name.name its logic that I get a beautifull exception...
so the solution was to modify the dataproto macro
{% macro proDataType(classe, value) %}
<tr class='block-to-remove'>
<td><button class='btn btn-default btn-delete-row'><span class='glyphicon glyphicon-minus'></span></button></td>
{% for truc in value %}
<td>{{ form_widget(truc, { 'attr': { 'class':'form-control' } }) }}</td>
{% endfor %}
</tr>
{% endmacro %}
and to send a arrayVar.vars.prototype to it