Problem with regex in product name (TPL prestashop 1.6) - prestashop-1.6

I need put regex ^.*gold|Gold|GOLD.* in product.tpl (Prestashop 1.6) and check if product name consist gold or Gold or GOLD. If product name consist in product name for example "Anchor blue Gold" then I need create product flag. Is that possible? I try it, but function is not working correctly.
{if (preg_match('^.*gold|gold|GOLD.*', {$product->name|escape:'html':'UTF-8'}))}
<span class="new-box">
<span class="zlato-label">GOLD 24K</span>
{elseif(preg_match('^.*rosegold|Rosegold|ROSEGOLD.*', {$product->name|escape:'html':'UTF-8'}))}
<span class="new-box">
<span class="zlato-label">Rose GOLD 24K</span>
{else}
<span class="new-box">
<span class="new-label">Nothing</span>
{/if}

Related

Thymeleaf classappend for more than two alternatives

I have a badge used inside the bootstrap card. There are 3 possible values for this badge. FREE, BASE, PREMIUM.
I want to use different colors for these 3 values.
Div class looks like this:
<div class="badge text-white position-absolute" th:text="${content.getCategory()}"></div>
How can I update my class on the fly to use bg-dark for BASE, bg-primary for FREE, and bg-danger for PREMIUM?
I tried to use literal substitutions for th:classappend but I could not find a way to make it work with triple possible values.
You could do it like this:
<div class="badge text-white position-absolute"
th:with="category=${content.category.toString()}"
th:classappend="|${category == 'BASE' ? 'bg-dark' : ''}${category == 'FREE' ? 'bg-primary' : ''}${category == 'PREMIUM' ? 'bg-danger' : ''}|"
th:text="${category}"></div>
This also works by defining an inline map:
<div class="badge text-white position-absolute"
th:with="categoryClass=${ {'BASE': 'bg-dark', 'FREE': 'bg-primary', 'PREMIUM': 'bg-danger'} }"
th:classappend="${categoryClass.get(content.category.toString())}"
th:text="${content.category}"></div>

Thymeleaf : 2 loops and an if statement inside a single element

In thymeleaf, I have a for each loop like so:
<ul class="days">
<li th:each="day : ${days}" th:text="${day}" >1</li>
</ul>
This successfully lists all of the strings in an array called days which is populated like ["1","2","3"..."31"] representing the days in a given month.
I also have an array of items which also contains days as strings.
Here is what I want to do in pseudo code but am struggling to figure out how to achieve it:
For each day in days; For each day in items ; if items.day =
days.day then set 'class=active' (bootstrap) and th:text =days.day
else
th:text=days.day
So if theres a day in items that matches a day in days then the <li> element is set to class=active and make the <li> clickable with href="/myurl". And either way the day from days is the th:text of the <li>.
Sorry if thats hard to understand, I tried to make it as clear as I could.
EDIT:
This is latest attempt:
<ul class="days">
<li th:each="s : ${days}" th:with="found=${false}">
<span th:each="item : ${items}" th:if="{$item.day == s}">
<span th:text="${s}" th:classappend="active" th:href
th:with="${found} = true"></span>
</span>
<span th:if="{$found == false}">
<span th:text="${s}"></span>
</span>
</li>
</ul>
I think you can achieve this with conditional expressions. Simply write something like
<span th:text="${s}" th:classappend="${#lists.contains(items, s)}? 'active' : ''">
This code also uses #lists, a very useful feature of Thymeleaf.

Grails: Removing Unwanted Brackets when Displaying Variable

I have a .gsp page where a student can select a course that they are in. That selection is then stored in an array list. I have another .gsp page that shows student details and within those details it shows which course they selected from the other page. However, when it displays the course it displays like this: "[courseName]", but I would like it to display without the brackets: "courseName".
This is my code for displaying the selection:
<g:if test="${studentDetails?.course}">
<li class="fieldcontain">
<span id="course-label" class="property-label">
<g:message code="student.course.label" default="Course(s)" /></span>
<span class="property-value" aria-labelledby="course-label">
<g:set var="course" value="${studentDetails?.course.courseName}" />
<g:message message="${course}" /></span>
</li>
</g:if>
So far I've tried displaying the variable with g:fieldValue, g:message, and just the variable itself without a tag. All methods display with the brackets. Any suggestions on how to remove the brackets are appreciated. If any other code is needed I can provide it. Thanks.
If your studentDetails?.course.courseName contains a List of courses and your want to display all of them, you need to convert it to a String. But default implementation of List.toString() uses brackets. Your could use .join(',') instead.
Like:
<g:if test="${studentDetails?.course}">
<li class="fieldcontain">
<span id="course-label" class="property-label">
<g:message code="student.course.label" default="Course(s)" /></span>
<span class="property-value" aria-labelledby="course-label">
${studentDetails.course.courseName.join(', ')}
</span>
</li>
</g:if>
Also I suggest to add .encodeAsHTML() if you got this data (course name) from a user, to escape any HTML content inside variables (avoid XSS, etc). Like:
${studentDetails.course.courseName.join(', ').encodeAsHTML()}

how can i make cucumber test on multiple 'a' tag with image

this is my view code where i have make multiple 'a' tag and want to test third element from li. and we can uniquely identified with offer id as per below code ...
<div class="search_data_outer_div">
<ul>
<li class="small_preview">
<div class="image_area">
<a href="/offer/show/334">
</div>
<span>
<span class="artist_name">Artist 1</span>
<span class="remaining_time">Remaining Time: 7 days</span>
<i id="334" class="icon-remove pointer" style="position:absolute;right:0;display:none;"></i>
</span>
</li>
and i have tried make one step defination ... it is working fine with cucumber but when execute with selenium (WebDriver) then page not open after click.
Scenario:
When I press third offer
Then I should see "YOUR OFFER"
and its step defination file
When /^I press third offer$/ do
page.execute_script %Q{ $(".search_data_outer_div ul li .image_area a").eq(2).click(); }
end
let me know right solution if anybody can help
thanks
IIRC CSS style selectors have issues with some version of Web Driver. Ideally you'd have PageObject variables set up for this so you're not using raw webdriver code in your steps but something like this should work:
browser.div( :class => 'search_data_outer_div').div( :class => 'image_area').links.first.click

Xpath and Nokogiri: why can't I get this attribute?

I am trying to parse HTML that looks like:
<p class="row">
<span class="itemdate"> Jul 22</span>
<span class="itemsep"> - </span>
$2000 / 3br - Three bedroom apartment/new renovtion
<span class="itemsep"> - </span>
<span class="itempn"><font size="-1"> (Bushwick-L Train-Close to Williamsburg)</font></span>
<span class="itempx"> <span class="p"> pic</span></span>
<span class="itemcg" title="abo"> <small class="gc">apts by owner</small></span>
</p>
When I perform this call:
page.xpath("//p[contains(#class, 'row')]/a").first
I get a result like:
#<Nokogiri::XML::Element:0x3feea2631444 name="a"
attributes=[#<Nokogiri::XML::Attr:0x3feea2631390 name="href"
value="http://newyork.craigslist.org/brk/abo/3102470187.html">]
children=[#<Nokogiri::XML::Text:0x3feea2630ad0 "$2000 / 3br - Three bedroom">]>
So by looking at the xpath docs I should be able to just return the url by doing something like:
page.xpath("//p[contains(#class, 'row')]/a#href").first
But I get this error:
Nokogiri::XML::XPath::SyntaxError Exception: Invalid expression:
//p[contains(#class, 'row')]/a#href
I know how to get the URL from the Nokogiri object, but I'd rather that the xpath search just return an array of URLs.
You're missing a slash, you need .../a/#href.

Resources