Laravel If Else statement inside foreach - if-statement

I'm trying to use if/else statement inside my foreach loop.
With DomDocument i'm trying to find the description tag and the inside of the description tag.
This works fine. When there is a description tag, it display's what's inside the description tag. If there is no description tag it displays: No description found No description found No description found No description found. Why does it show the error four times? How do i fix this?
I'm using DomDocument and Laravel 5.4
Meta tag:
$meta = $dom->getElementsByTagName('meta');
My code:
#foreach ($meta as $node)
#if($node->getAttribute('name')=='description')
{{$node->getAttribute('content'), PHP_EOL}}
#else
{{'No description found'}}
#endif
#endforeach
Edit, according to Muthu17:
I have almost written the same code as you. But the only problem is: It shows "No description found" all the time. Even if it finds the description tag, it show's the description tag init and "No description found".
I've declared the $showErrorMessage = 0; in my controller.
#foreach ($meta as $node)
#if($node->getAttribute('name')=='description')
{{$node->getAttribute('content'), PHP_EOL}}
#else
{!! $showErrorMessage == 1; !!}
#endif
#endforeach
#if($showErrorMessage = 1)
{{'No description found'}}
#endif

Hope you are looking something like this
<?php $showErrorMessage = 0; ?>
#foreach ($meta as $node)
#if($node->getAttribute('name')=='description')
{{$node->getAttribute('content'), PHP_EOL}}
#else
<?php $showErrorMessage = 1;?>
#endif
#endforeach
#if($showErrorMessage == 0)
{{'No description found'}}
#endif

Related

Opencart Check if current page is product

Does OpenCart Version 2.3.0.2 have some methods to check in header or footer .tpl files that current page is product? Example if (is_product) { // do something }
You can try to check if your session has product. $this->session->data['product_id'] You can check it anywhere. You can write the product to the session... for example:
$this->session->data['product_id'] = /*your product_id*/;
And now you can retrieve this product anywhere.
No, But you can create it.
File:
catalog/controller/common/header.php
Find:
return $this->load->view('common/header', $data);
Add Before:
$data['is_product'] = isset($this->request->get['route']) && $this->request->get['route'] == 'product/product';
File:
catalog/view/theme/default/template/common/header.tpl
Add where you need:
<?php if (!empty($is_product)) { ?>
<p>This is the product page</p>
<?php } else { ?>
<p>This is not the product page</p>
<?php } ?>

how to put opencart 3.0 customer name on header?

Can Any one help me how to display customer name in header after
customer's login
I also try some code but dosen't work
in controller>common>header.php
$data['customer_firstname'] = $this->customer->getFirstName();
$data['customer_lastname'] = $this->customer->getLastName();
Thank you in advance
You need to edit these files:
Controller catalog/controller/common/header.php
Find: return $this->load->view('common/header', $data);
Add before it:
if($this->customer->isLogged()){
$data['text_customer_name'] = sprintf($this->language->get('text_customer_name'), $this->customer->getFirstName(), $this->customer->getLastName());
} else {
$data['text_customer_name'] = $this->language->get('text_account');
}
Language catalog/language/*/common/header.php
Add: $_['text_customer_name'] = 'Hi %s %s';
View catalog/view/theme/*/template/common/header.twig
Find: {{ text_account }}
Replace with: {{ text_customer_name }}

Move "Submitted By" from node.tpl to page.tpl

I'm looking to move "Submitted By" info from node.tpl to page.tpl however when I add the following from node.tpl I get errors. Im assuming I dont have access to those variables, but would like to know how I can set up a pre-proccess to get it to display as it does in the node.tpl
<?php if ($display_submitted): ?>
<div class="submitted">
<?php print $submitted; ?>
</div>
<?php endif; ?>
You can either use a preprocess function in the template.php of your theme, as explained here:
https://drupal.stackexchange.com/questions/40222/how-can-i-print-node-authors-last-login-date-on-page-tpl-php
In your case it would look like this (tested on Drupal 7):
function yourtheme_preprocess_page(&$variables) {
$variables['author'] = "";
if (isset($variables['node']) && ($account = user_load($variables['node']->uid))) {
$variables['author'] = $account->name;
}
}
And then in your page.tpl.php use this:
Submitted by: <?php print $author; ?>
If you don’t want to touch any of your theme’s files, but you need to output the author’s name in another region as the node content, you could create a view (block display) containing the node author, and assign it to the region.
While normally done in node.tpl.php, if the page is a node view page, the $node variable is also available in page.tpl.php
You can then use something like:
if (isset($node)) {
// Check if display submitted variable is set for this node type
if (variable_get('node_submitted_'. $node->type, 0)) {
// Do stuff
}
}
An alternative approach would be adding the required logic instead to an implementation of
hook_preprocess_page
Bonus update: You can see the $node variable added to page.tpl.php in core template_preprocess_page
if ($node = menu_get_object()) {
$variables['node'] = $node;
}
In page.tpl.php:
<div class="submitted">
<?php echo format_date($node->created, 'custom','d.m.Y'); ?><br />
<?php echo 'by ' . $node->name; ?>
</div>

Liferay portal 6.2 - Asset publisher template for showing Knowledge Base Articles

Hi I am trying to create a custom template for Asset Publisher portlet, to show my Knowledge Base Articles. For doing this, I need to retrieve
Entry title
Entry content
Create Date
I can find the title and the date but there's no method to get the content. Any help?
##
## This template can be used to modify the look of a specific
## application.
##
## Please use the left panel to quickly add commonly used variables.
## Autocomplete is also available and can be invoked by typing "$".
##
#if (!$entries.isEmpty())
#foreach ($curEntry in $entries)
<div>
<p>News 1:<br/>
$curEntry.getTitle($locale) <br />
$curEntry.getCreateDate() <br />
$curEntry.getDescription()<br /></p>
</div>
#end
#end
As you can see in the code above, I tried "getDescription()" but it doesn't work.
$curEntry is an AssetEntry instance, it's not the a Knowledge Base Article.
You can use $curEntry.getClassPK() to retrieve the Knowledge Base Article instance using Knowledge Base Article service.
If you check AssetEntry table in database you will find description column is empty, so you will not get any data you want.
You can get asset data by using below code:
#foreach ($curEntry in $entries)
Title: $curEntry.getTitle($locale)<br/>
Published date: $entry.getPublishDate()<br/>
Summary: **$entry.getAssetRenderer().getSummary($locale)** <br/>
#end
Now how above code is working. Summary we want, do lie in JournelArticle class(table). Interface AssetRender is implemented by class JournalArticleAssetRenderer. This class inherits getSummary(Locale locale) method. This method contains object of JournetArticle class.
JournelContent obj;
public String getSummary(Locale locale) {
return obj.getDescription(locale);
}
You can use the following code:
#if (!$entries.isEmpty())
<div class="news">
#foreach ($entry in $entries)
#set($renderer = $entry.getAssetRenderer() )
#set($className = $renderer.getClassName() )
#if( $className == "com.liferay.portlet.journal.model.JournalArticle" )
#set( $journalArticle = $renderer.getArticle() )
#set( $document = $saxReaderUtil.read($journalArticle.getContent()) )
#set( $rootElement = $document.getRootElement() )
#set( $xPathSelector = $saxReaderUtil.createXPath("dynamic-element[#name='image']") )
#set( $image = $xPathSelector.selectSingleNode($rootElement).getStringValue() )
#set( $link = $renderer.getURLViewInContext($renderRequest, $renderResponse, '') )
<div class="new"">
<h1 class="title">$entry.getTitle($locale)</h1>
<div"><img src="$image" ></div>
Read More
</div>
#end
#end
</div>
#end

Regular Expression issue

I have a code like this
<div class="rgz">
<div class="xyz">
</div>
<div class="ckh">
</div>
</div>
The class ckh wont appear everytime. Can someone suggest the regex to get the data of fiv rgz. Data inside ckh is not needed but the div wont appear always.
Thanks in advance
#diEcho and #Dve are correct, you should learn to use something like the native DOMdocument class rather than using regex. Your code will be easier to read and maintain, and will handle malformed HTML much better.
Here is some sample code which may or may not do what you want:
$contents = '';
$doc = new DOMDocument();
$doc->load($page_url);
$nodes = $doc->getElementsByTagName('div');
foreach ($nodes as $node)
{
if($node->hasAttributes()){
$attributes = $element->attributes;
if(!is_null($attributes)){
foreach ($attributes as $index=>$attr){
if($attr->name == 'class' && $attr->value == 'rgz'){
$contents .= $node->nodeValue;
}
}
}
}
}
Regex is probably not your best option here.
A javascript framework such as jquery will allow you to use CSS selectors to get to the element your require, by doing something like
$('.rgz').children().last().innerHTML