Passing Variables to Smarty view file - php

Hello I'm trying to send a value from my php file to tpl file.
when I print_r in php file I see my value is defined in array however when I print_r my tpl file I don't see this element.
<img src="{$product.manufacturer_name}" alt="" title="" itemprop="image" />
and my controller
$special = Product::getProducts((int)$this->langID , 0,6, 'name', 'DESC', 51);
foreach ($special as $specia)
{
$id_image = Product::getCover($specia['id_product']);
// get Image by id
if (sizeof($id_image) > 0)
{
$image = new Image($id_image['id_image']);
// get image full URL
$image_url = _PS_BASE_URL_._THEME_PROD_DIR_.$image->getExistingImgPath()."-home_default.jpg";
$specia['manufacturer_name']=$image_url;
}
}
$dir = _PS_MODULE_DIR_.'/ptspagebuilder/views/templates/front/widgets/sub/products.tpl';
$tdir = _PS_ALL_THEMES_DIR_ . _THEME_NAME_ . '/modules/ptspagebuilder/views/templates/front/widgets/sub/products.tpl';
if (file_exists($tdir)) {
$dir = $tdir;
}
// get Product cover image (all images is possible retrieve by
// Image::getImages($id_lang, $id_product) or
// $productInstance->getImages($id_lang))
$setting['product_tpl'] = $dir;
$setting['products'] = $special;
$output = array('type' => 'flashsale','data' => $setting);
return $output;
}

if $special contain arrays, than you have mistake in script, change copies of vars in foreach loop. Try to change
foreach ($special as $specia) to foreach ($special as $k=>$specia)
$specia['manufacturer_name']=$image_url; to $special[$k]['manufacturer_name']=$image_url;

Actually i didn't get your problem. You said that you assigned value in your php file but i can't see any value which is assigned. For assigning value in php file you have to assign value in smarty variable so that you can access it in your tpl file. For that you have to write code something like that:
In your controller first You have to assigned value like:
$this->smarty->assign(array(
'product' => $containing_product,
));
The $product contain the value which you want to access in your tpl file.
Now in your tpl file you can check it like:
{var_dump($product)}
And Now in your tpl file you can use that
<img src="{$product}" alt="" title="" itemprop="image" />

Related

value of variable vanishes

I do not understand what is happening with my variables, the values all vanish / become empty within the function. I don't have anything within the function. I believe that I am doing the 'checks' correctly '==' and the assignments correctly '='. Regardless of what i try, i continue to get the 'file doesn't exist result'. and if i var_dump to see what the value of $cID or $check, they come back as empty ''.
function gt_masthead($cID='3', $check=false, $str=''){ //assign value to var
$check == file_exists('./' . $cID . 'bg-userProfile.jpg'); //file exists?
//echo '<h1> / ' . $cID . ' / ' . $check . ' </h1>';
dumpVar($cID); //test
if($check == 1){
// file exists, show
$str = '<img class=" img-fluid" style="width: 100%;" src="./3bg-userProfile.jpg" alt="Card image cap">';
}else{
// file does not exist, show default
$str = '<img class=" img-fluid" style="width: 100%;" src="./img_logoMarvel.gif" alt="Card image cap">';
}
return $str;
}
Try this code instead:
<?php
function gt_masthead($cID, $image = "bg-userProfile.jpg", $default = "img_logoMarvel.gif") { //assign value to var
//check if given image file exists
if(file_exists("./{$cID}{$image}")) {
//if it does exist, set the displayImage variable to the path of the image.
$displayImage = "./{$cID}{$image}";
} else {
//if it does not exist, set the displayImage variable to the path of the default image
$displayImage = "./{$default}";
}
//Add the displayImage path to an image element, and return that element.
$str = "<img class='img-fluid' style='width:100%;' src='{$displayImage}' alt='Card image cap'>";
return $str;
}
?>
The usage of this code is pretty simple. You call it like this:
gt_masthead("cID number", "image name"); - you can also use variables here, just drop the quotes.
Alternatively, you can also use a 3rd variable to dynamically set the default image if the searched one does not exist.
gt_masthead("cID number", "image name", "default image name");
I commented the code to try to explain what is going on. If you have any questions please ask
Here is a more standard way to write the function, I know the squiggly braces sometimes confused people, but the code is basically the same.
<?php
function gt_masthead($cID, $image = "bg-userProfile.jpg", $default = "img_logoMarvel.gif") { //assign value to var
//check if given image file exists
if(file_exists("./".$cID.$image)) {
//if it does exist, set the displayImage variable to the path of the image.
$displayImage = "./".$cID.$image;
} else {
//if it does not exist, set the displayImage variable to the path of the default image
$displayImage = "./".$default;
}
//Add the displayImage path to an image element, and return that element.
$str = "<img class='img-fluid' style='width:100%;' src='".$displayImage."' alt='Card image cap'>";
return $str;
}
?>

if file_exists in PHP issue wordpress

I am attempting to create code that checks if an image exists on my site and if not shows a default image. In one case I know the file exists and can get it to link to the file using the variable that I want file_exist to use!
$menuCategories = get_categories( array(
'child_of' => $whichGrade,));
foreach ( $menuCategories as $menuCategory ) { ?>
<?php
$linktoicon = get_bloginfo('template_directory') ."/images/menuicon_".$menuCategory->slug.".png";
if (file_exists($linktoicon)) {
$iconref = $menuCategory->slug;
}else {
$iconref = "default";
} ?>
<a href='<?php echo $linktoicon; ?>'> <?phpvar_dump($iconref); ?></a>
<?php }?>
$linktoicon` is "http://mybritishhelper.com/wp-content/themes/wpex-magtastico/images/menuicon_colours.png"
Thank you.
PHP's file_exists isn't used with URL's, it's used for paths to files on the server itself. Not a problem. First, we need to get the path to your theme's template directory:
$templateDirectory = get_template_directory();
Assuming that the link you provided is on your own server, the full path to your image is
$pathToImage = $templateDirectory . '/images/menuicon_colours.png';
We can now check if your image exists with the following
if (file_exists($pathToImage)) {
// Do stuff
}
Hope this helps. For reference, see the docs for file_exists and get_template_directory()

How to get the intro image of a joomla article

I am trying to customize the the category list of my website. So I have a menu option that shows the list of all article is a certain category.
In this list I would like to show the into image of the article too. But unfortunately I cannot find the path of this intro image of each article to put it in the '' tag.
This code takes the first image in the article. Which is not what I want.
<?php
$text = $article->introtext;
if(preg_match('/<\s*img[^>]+>/Ui', $text, $matches)){
$image=$matches[0];
$text = str_replace($image,'',$text);
}
if ($matches) {
$image = str_replace('img src="','img width="150px" src="/demo/persianlondon/',$image);
$image = str_replace("caption","", $image);
echo '' . $image . '';
}
else {echo '<img width="150px" src="http://goo.gl/k47rNN" class="defaultArticleImage" />';}
?>
So I need to get the intro image url and set it for $image.
Any idea to do it?
If you are talkin about blog view of a category, this code will write out the intro image filename with path, if you put it in file:
jroot/component/com_content/views/category/tmpl/blog.php or in the template overrided blog.php file.
$article_items = $this->get('lead_items');
foreach ($article_items as $item) {
$imageObj = json_decode($item->images);
echo $imageObj->image_intro;
}
other variables of $imageObj:
stdClass Object
(
[image_intro] =>
[float_intro] =>
[image_intro_alt] =>
[image_intro_caption] =>
[image_fulltext] =>
[float_fulltext] =>
[image_fulltext_alt] =>
[image_fulltext_caption] =>
)

Adding array to a model file in Codeigniter

I have the following array in my view file (which works):
<?php
$images = array(img("slideshow/demo1.png", "one"),img("slideshow/demo2.png", "two"),img("slideshow/demo4.png", "three"));
$captions = array("first image", "second image", "third image");
foreach (array_combine($images, $captions) as $image => $caption ) {
echo "<li>", $image, "<div class=\"orbit-caption\">",$caption,"</div></li>";
}
?>
However I wish to use the same foreach in a slideshow file across multiple pages with different images displaying so i thought the simplest way would be to create a model page for each page that required different data and having the images and captions in an array as above. I have tried searching how to do this but all the solutions I have found relate to being in a database which is unfortunately not an option.
Also I would like to code it so that if there is not image caption the caption code does not run ie:
"<div class=\"orbit-caption\">",$caption,"</div>
The options I have tried either I get a blank semi transparent box ie it puts down:
<div class="orbit-caption"></div>
Or I getting nothing if I have a caption or not.
I understand a small amount of php but am more of a designer and am fairly new to MVC so will not be offended if anyone has a solution in an idiots guide :) just trying to learn. If anyone has a better solution then please share
Create a helper in application/helpers folder and name it slideshow_helper.php, Now, in slideshow_helper.php write :
<?php
function get_image_array(){
$images = array(img("slideshow/demo1.png", "one"),img("slideshow/demo2.png", "two"),img("slideshow/demo4.png", "three"));
$captions = array("first image", "second image", "third image");
return array_combine($images, $captions);
}
?>
In your controller function from where you load the view for slideshow page, write:
<?php
$data = array();
$this->load->helper('slideshow');
$data['slideshow'] = get_image_array(); #will return the image array from the helper
$this->load->view('view', $data); #load the image array in the view
?>
In your View:
<?php
foreach ($image as $imageSrc => $caption ) {
if($caption != ""){
echo "<li>", $imageSrc, "<div class=\"orbit-caption\">",$caption,"</div></li>";
}
}
?>

Get last section from echo and store in variable PHP

I've a function that will echo the URL of the image from the content of Wordpress.
I got the function work now no problem
// Get Image Attachments
function sa_get_image($postid=0, $size='thumbnail') { //it can be thumbnail or full
if ($postid<1)
$postid = get_the_ID();
$thumb = get_post_meta($postid, "thumb", TRUE); // Declare the custom field for the image
if ($thumb != null or $thumb != '') {
echo $thumb;
}
elseif ($images = get_children(array( //If you upload an image function gets first image
'post_parent' => $postid,
'post_type' => 'attachment',
'numberposts' => '5',
'post_mime_type' => 'image', )))
foreach($images as $image) {
$thumbnail=wp_get_attachment_image_src($image->ID, $size);
?>
<?php echo $thumbnail[0]; ?>
<?php }
else { //If you don't upload or declare as thumb custom field func. gets custom (default) image
echo get_bloginfo ( 'template_directory' ); //same as wp-content/themes/your-theme/
echo '/images/image-pending.gif'; // Put this image into your themes images folder and set the path here
}
}
The only problem now is that the <?php echo $thumbnail[0]; ?> if there are more than one image it will echo all of them something like this
<img src=" http://applesiam.com/wp-content/uploads/2555-05-02_19h14_34-150x150.png http://applesiam.com/wp-content/uploads/2555-05-02_19h14_11-150x150.png http://applesiam.com/wp-content/uploads/2555-05-02_19h13_43-150x123.png http://applesiam.com/wp-content/uploads/2555-05-02_19h13_20-150x150.png http://applesiam.com/wp-content/uploads/2555-05-02_19h13_17-150x150.png ">
As you can see it just separated by some spaces.
Now I just want to have the last image if there is more than one image in the $thumbnail
I'm not really expert with PHP as my semester for PHP course will start next week.
Thanks in advance for any suggestion going to be.
Try:
$imgSrc = end((explode(' ', trim($imgSrc)));
Where $imgSrc is the value you put into <img src="!!!==>>here<<==!!!">.
quickly typed, w/o any warranty. Should leave single URLs intact, and if multiple ones separated by space(s) will take the last.
Try this instead.
echo trim($thumbnail[sizeof($thumbnail)-1]);

Categories