increase size of excerpt in shopify - php

i am displaying blog articles in some text and at the end Read more button in order to view complete article in my shopify web
this is the code that i am using
<div class="rte">
{% if article.image %}
{% assign image_alt = article.title | escape %}
<p>{{ article | img_url: '1024x1024' | img_tag: image_alt, 'article__image' | link_to: article.url }}</p>
{% endif %}
{% if article.excerpt.size > 0 %}
{{ article.excerpt }}
{% else %}
<p>{{ article.content | strip_html | truncatewords: 100 }}</p>
{% endif %}
</div>
it worked very well now i set only one article to display
{% paginate blog.articles by 1 %}
it displayed only one article.
i just want to increase the short description text of article to 50% with read more button.
is this possible to increase the size of article.excerpt so that i will display more text with read more button
i tried this but this is not work for me
{{ article.excerpt.size = 1200 }}

Related

How to run For loop in twig file in php opencart to make the results should be like below

Suppose,
$a=1,2,3,4,5,6,7,8,9,10
then
in the for loop, it should run 1,2,3,4
and then 5 then 6,7,8,9 and then 10
then exit the loop.
You could use the batch and slice Twig filters for this. If you batch the results by groups of 5, then slice the first 4 and the 5th element you are able to loop through the results in the way you want.
Example code:
{% set items = [1,2,3,4,5,6,7,8,9,10] %}
{% for batch in items|batch(5) %}
<p>item 1 through 4</p>
{% for item in batch|slice(0, 4) %}
{{ item }}
{% endfor %}
<p>5th item</p>
{% for item in batch|slice(4, 1) %}
{{ item }}
{% endfor %}
{% endfor %}

Next article section not working in craftcms anyone know what went wrong here?

one of my craft cms projects, I have articles and once click articles I can read those articles, and in that right side content I have a tab call Next article, but when clicking that nothing response
here the live site - http://ambitmagazine.co.uk/poems/ambit-poetry-competition-2018
screenshot - https://prntscr.com/o9gkph
_entry.html
{% extends "_layout" %}
{% block content %}
{% set issue = entry.issue.first() %}
{% set nextArticle = craft.entries.section('articles').after(entry.postDate).order('postDate asc').limit(1).find() %}
<div class="article section{% if entry.largeText %} article--larger-text{% endif %}">
<div class="article__inner section__inner">
{% include 'articles/_partials/article-header' with { article: entry, issue: issue } only %}
{% include "articles/_types/" ~ entry.type %}
{% if entry.relatedAuthor|length > 0 %}
{% include 'articles/_partials/article-footer' with { article: entry } only %}
{% endif %}
{% if nextArticle|length > 0 %}
<div class="next-item">
<a href="{{nextArticle[0].url}}" class="next-item__inner">
<span><strong>Next Artcile</strong></span>
<span>\</span>
<span>{{nextArticle[0].title}}</span>
</a>
</div>
{% endif %}
<div class="article__sidebar-inner"></div>
</div>
<div class="article__sidebar-outer"></div>
</div>
.after is doing a >= operation so the current article is included in the results, and is almost certainly going to be the first option.
Doing something like .after(entry.postDate|date_modify('+1 second')) will exclude the current article and should give you what you are looking for.
It may be more clear for your query to keep the time as is, but explicitly exclude the current entry by id:
{% set nextArticle = craft.entries()
.section('articles')
.after(entry.postDate)
.id(['not', entry.id])
.order('postDate asc')
.one() %}

Print Twig variable x times based upon randomized range

I am using Twig and Timber for a WordPress project. I have the following loop in my template that prints my custom post type titles into a HTML structure.
{% for company in companies %}
{% set dot = "<div class='company-dot'></div>" %}
{% set range = range(10, 20) %}
{{dot}}
{{random(range)}}
<div class="company">
<div class="company-dot dot-active"></div>
<p class="dot-caption">{{ company.title }}</p>
</div>
{% endfor %}
I would like to print my {{dot}} variable x amount of times based upon the number that is generated by {{random(range)}}. How can I do this?
The simplest solution would be to iterate random(range) times with a for loop:
{% for i in 0..random(range(10, 20)) %}
{{ dot }}
{% endfor %}
I don't really know Twig but my guess is that you could to the following:
{% for i in random(range) %}
{{dot}}
{% endfor %}
You already know how to use range, just use it again :
{% set dots_count = random(range) %}
{% for dot_index in range(1,dots_count) %}
{{dot}}
{% endfor %}
{{dots_count}}
Here is a fiddle : https://twigfiddle.com/ko595z

PHP - Twig change array key value

I am new to Twig and looking for a solution for the following (which would be very easy in PHP, however, our templates are setup in Twig)
What I'm trying to do
Edit an array value (using its index) in Twig so that it can be output after a loop.
What is happening
The array value (retrieved using its index) does not change when I try to edit the array value by index. Instead, it may append the value to the array
My code
...
{% set amount = [0,0,0] %}
{% for invoice in invoices %}
<tr>
<td>{% if invoice.age <= 10 %}{% set amount ?????? %}{% endif %}</td>
<td>{% if invoice.age > 10 and invoice.age <= 20 %}{% set amount ?????? %}{% endif %}</td>
<td>{% if invoice.age > 20 %}{% set amount ?????? %}{% endif %}</td>
</tr>
{% endfor %}
...
{{ amount[0] }}
What I've tried
I have tried the following to change the value of age[0] to no avail.
{% set amount = amount|merge({0: 'test'}) %}
{% set amount = amount|merge({0: 'test'})|keys %}
{% set amount = amount|merge({(0), 'test'}) %}
{% set amount = amount|merge({(0), 'test'})|keys %}
... and many more.
Intended outcome
I want to be able to output {{ age[0] }} at the end to display the amount total of all invoices aged 10 or less. Similarly, I would also like to output age[1] and age[2] to display amount total for all invoices aged between 10 and 20 days, and over 20 days, respectively.
In twig, always keep everything simple, without hard logic.
In your case, just create 3 variables.
{% set amount_under_10, amount_between_10_and_20, amount_over_20 = 0,0,0 %}
{% for invoice in invoices %}
<tr>
<td>{% if invoice.age <= 10 %}{% set amount_under_10 = amount_under_10 + 1 %}{% endif %}{{ invoice.age }}</td>
<td>{% if invoice.age > 10 and invoice.age <= 20 %}{% set amount_between_10_and_20 = amount_between_10_and_20 + 1 %}{% endif %}{{ invoice.age }}</td>
<td>{% if invoice.age > 20 %}{% set amount_over_20 = amount_over_20 + 1 %}{% endif %}{{ invoice.age }}</td>
</tr>
{% endfor %}
{{ amount_under_10 }}
{{ amount_between_10_and_20 }}
{{ amount_over_20 }}
See fiddle
If you need to be more generic (arbitrary number of ranges for example) don't do it in Twig. Twig is made for rendering information, no more.
See my answer here for a different approach.
I think I understand the idea behind keeping logic out of templates, but in my case I have SQL queries written by the administrator that are executed and a generic template outputs the results.
It makes more sense to calculate totals in the template.

How to say "if this translation is empty"

I want to have this image not showing if the translation is empty or non existing
<img class="partners-logo" src="{{ 'page.image.path' | trans | raw }}">
So can I wrap a logic loop around it as in the following code?
{% if {{ 'page.image.path' | trans }} is not null %}
<img class="partners-logo" src="{{ 'page.image.path' | trans | raw }}">
{% endif %}
Obviously not right? Then how should it be?
You can do something like that :
{% if "page.image.path"|trans != "page.image.path" %}
This will check if the result of the translation is different from the translation key : if a translation key has no translation, filter trans returns the translation key.

Categories