I have content stored in variables and I have a string that users enter in search fields.
My question is how can I chop the text before and after the searched string?
-SearchString has a the value which user has entered.
-Wholetext has the whole data from database.
Now, I want to show the Wholetext as the excerpt like user search for "test" and then I will show the result like:
"text before search string" : "test"
"text after search string" : "_"
Here's my code:
{% block field %}
<div>
{% set SearchString=admin.datagrid.filters.data.value.value %}
<div class="less_resume_container">
{% if SearchString is defined and SearchString in object.data %}
{% set Wholetext= object.data|replace({ (SearchString): '<span style="background-color: #FFFF00;font-size:15px;font-weight:bold">' ~SearchString~'</span>'}) %}
{{ Wholetext|striptags()|truncate(50) }} <a href="javascript:void(0)" class="show_full_resume">Show
more</a>
{% else %}
{% set Wholetext= object.data %}
{{ Wholetext|truncate(50) }} Show more
{% endif %}
</div>
<div class="full_resume_container" style="display: none;">{{ Wholetext|raw() }}
View less
</div>
<br/>
</div>
{% endblock %}
Currently it is showing 50 characters from the text.
This is not what I want. By the way I am using sonata admin with symfony 2.
You can use a combination of substr and strlen.
substr(WHOLETEXT, 0, (strlen(WHOLETEXT) - strlen(SEARCHTERM)))
Alternatively, you could explode the whole text on the search term, which would leave you with a two part array(assuming your search term only appeared once). The first part would be everything before the search term. The second part would be everything after.
$array = explode(SEARCHTERM, WHOLETEXT);
$before = $array[0];
$after = $array[1];
Related
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() %}
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
I am having trouble working around with twig.
I pass a $labelz var on twig, and this $labelz is an array of the form label[0][left], label[1][left], ... label[2][left] so on.
This array, $labelz is actually a _POST var, so when I do some "validation", i want to echo the values stored on this array again back to the form. Hence, I loop it on twig. I can successfully echo the stored value back in a input form using
{% for key, label in labelz %}
<input type="text" name="label[{{ key }}][left]" value="{{ labelz[key]['left'] }}">
{% endfor %}
Now, here is my problem, I am using this function
{% if errors.has('VARIABLE') %}
{{ errors.first('VARIABLE') }}
{% endif %}">
to check if there's an error passed to the page (using errors.has()), then echo the error message if it has (using errors.first()) . Now, with those function I want to do the same to $labelz so I tried using this:
{% for key, label in labelz %}
{% if errors.has(labelz[key]['left']) %}
{{ errors.first(labelz[key]['left']) }}
{% endif %}`
{% endfor %}
but it fails, because as you can see, and note key is a forloop variable of twig, and i have no idea how to make this work. Please help thanks!
turns our ~ solved my problem, it allows me to concatenate string on twig.
Thanks!
How do I make a simple count loop in Wordpress Timber(Twigg)?
So basically just a loop like this:
($i = 0;0 < 3;i++){
echo $test[i];
}
You could use
{% for value in test %}
{{ value }}
{% endfor %}
that is safer than
{% for i in 0..2 %}
{{ test[i] }}
{% endfor %}
because in second version you have to care about index (is setted? and so on) whereas in the first you don't.
Of course if your final goal is to print only three elements from the array you should consider slice filter
{% for value in test|slice(0, 3) %}
{{ value }}
{% endfor %}
Default radio widget creates a following structure:
<label>...</label>
<div id="...">
<div class="clearfix prettyradio labelright blue">
<input type="radio" id="..._0" name="..." value="..." style="display: none;">
...
</div>
I found the radio_widget block, but it contains only an input itself. So I can customize there only this part:
<input type="radio" id="..._0" name="..." value="1" style="display: none;">
But I can't understand how to change whole the structure of radio choice field?
Also, does anybody knows, why symfony adds display:none to the input?
Thanks.
if you're using Radio Field Type, you can customize only the input part of the radio_widget block by calling form_widget(form.yourField), all it displays is,
{% block radio_widget %}
{% spaceless %}
<input type="radio" {{ block('widget_attributes') }}{% if value is defined %} value="{{ value }}"{% endif %}{% if checked %} checked="checked"{% endif %} />
{% endspaceless %}
{% endblock radio_widget %}
But if you're using Choice Field Type to display Radio Fields (expanded => true and multiple => false). You'll then have to override the choice_widget block, which call for each child element the radio_widget block surrounded by a global div
How did you get the "display:none"? because there's no style such this in the default block.
If you specifically want to override the way an individual radio field is rendered - i.e. how each input field in the group is rendered - use this formula for the block name:
_<form name>_<field name>_entry_widget
Note this bit: _entry
If you're using an expanded choice field ..._entry_row and ..._entry_label won't work because they aren't used for the individual choices - well for radio buttons at least.
More generally you can find out a lot about which block Symfony intends to use during the next call to {{ form_widget(form) }} function call using this code:
{% for b in form.vars.block_prefixes %}
{{ dump(b) }}
{% endfor %}
or you might want to look at child in some situations:
{% for b in child.vars.block_prefixes %}
{{ dump(b) }}
{% endfor %}