Jump to content

[SOLVED]Module variables on different tpl files


Recommended Posts

Hello,

I can't figure out how to do it. For example - I have a gallery module and on the gallery page the tpl files creates img lists with this code:

 



{if $images}
<h2>{l s='Images' mod='azgallery'}</h2>

{assign var='nbItemsPerLine' value=3}

<ul id="image_list" class="gallery_list">
{foreach from=$images item=item name=images}
<li class="{if $smarty.foreach.images.index%$nbItemsPerLine==0}first-item span4{else}item span4 {/if}">
<a title="{$item.caption}" rel="lightbox" href="{$gallery_dir}images/{$item.id_image}-thickbox_default.jpg" class="lightboxTrigger">
<img src="{$gallery_dir}images/{$item.id_image}-large_default.jpg"></a>
<p class="caption">{$item.caption}</p>
</li>
{/foreach}
<br class="clear">
</ul>
{/if}

 

So when I copy the same line in different tpl file which isn't related to that module, it doesn't understand lines like

{foreach from=$images item=item name=images}

etc. So any ides what kind of files or links I have to include so the non-related tpl understands them?

Edited by R.Kinkeris (see edit history)
Link to comment
Share on other sites

 

So when I copy the same line in different tpl file which isn't related to that module, it doesn't understand lines like

{foreach from=$images item=item name=images}

etc. So any ides what kind of files or links I have to include so the non-related tpl understands them?

 

It does not understand those lines because variable $images is not defined in files(controllers) that are connected with those other tpl files.

If you check gallery.php you will probably find code that takes same data from database and then make array of images that is passed to tpl file as variable $images.

Link to comment
Share on other sites

That just call template file gallery.tpl, but for variables there must be some other code.

For example in ProductContraoller.php you have

 $images = $this->product->getImages((int)$this->context->cookie->id_lang);
 $product_images = array();
 foreach ($images as $k => $image)
 {
  if ($image['cover'])
  {
   $this->context->smarty->assign('mainImage', $images[0]);
   $cover = $image;
   $cover['id_image'] = (Configuration::get('PS_LEGACY_IMAGES') ? ($this->product->id.'-'.$image['id_image']) : $image['id_image']);
   $cover['id_image_only'] = (int)$image['id_image'];
  }
  $product_images[(int)$image['id_image']] = $image;
 }
.
.
.
if (count($product_images))
  $this->context->smarty->assign('images', $product_images);

 

So here is $product_images assigned to variable images that will be used in product.tpl file.

 

And to use it on different page, well that depends on where you what to add that gallery.

Maybe you could use some of existing hooks and add gallery.

Or you can override some controller...

Link to comment
Share on other sites

For example create new file in your root override/controllers/front named CmsController.php

And code should be something like

<?php
class CmsControllerCore extends CmsControllerCore
{
public function initContent()
{
 $this->getGallerry();
 parent::initContent();
}

public function getGallerry()
{
 // here goes code for getting images from gallery module
 $gallerry_images = ...;


 $this->context->smarty->assign('images', $gallerry_images);

 $this->setTemplate(_PS_MODULE_DIR_.'nameofgallerymodule/gallery.tpl');
}

}

 

Note this is not tested and you could try to follow logic from gallery.php.

Link to comment
Share on other sites

Ok, I'm still too weak at these things. Could you help me and note witch line is getting the images from gallery module? Here are two .php which refers to my module.

 

Both files are attached.

 

And this is what I'm using in cms page to fetch the images:

 



<ul id="image_list" class="gallery_list">
{foreach from=$images item=item name=images}
<li>
<a href="http://91.224.13.185/velosocks.com/index.php?fc=module&module=azgallery&controller=gallery">
<img src="{$module_dir}azgallery/images/{$item.id_image}-large_default.jpg"></a>
<p class="caption">{$item.caption}</p>
</li>
{/foreach}
</ul>

 

I would be very happy if you could help me with this. I'm newbie at these things.

azgallery.php

gallery.php

Edited by R.Kinkeris (see edit history)
Link to comment
Share on other sites

Ok here is controller, done in a bit of hurry and probably not the greatest solution.

But you can start from that.

 

<?php
class CmsController extends CmsControllerCore
{
public function initContent()
{
 $this->getGallery();
 parent::initContent();
}

public function getGallery()
{
 include_once(_PS_MODULE_DIR_.'azgallery/azgallery.php');
 $azgallery = new AzGallery;

 $this->context->smarty->assign(array(
  'gallery_url' => _PS_BASE_URL_.__PS_BASE_URI__.'index.php?fc=module&module=azgallery&controller=gallery',
  'gallery_dir' => _PS_BASE_URL_.__PS_BASE_URI__.'modules/azgallery/'
 ));

 /* List albums */
 if( !isset( $_GET['id_album'] ) )
  $this->context->smarty->assign('albums', $azgallery->getAlbums());

 /* List images */
 $this->context->smarty->assign('images', $azgallery->getImages( 1 ));

 if( isset( $_GET['id_album'] ) )
  $this->context->smarty->assign('backhome', true);
 $this->setTemplate(_PS_MODULE_DIR_.'azgallery/views/templates/front/gallery.tpl');
}
}

and in cms.tpl

<div class="rte{if $content_only} content_only{/if}">
 {$cms->content}
</div>
<ul id="image_list" class="gallery_list">
{foreach from=$images item=item name=images}
 <li>
  <a href="#">
  <img src="{$base_dir}modules/azgallery/images/{$item.id_image}-large_default.jpg" width="128" height="128"></a>
  <p class="caption">{$item.caption}</p>
 </li>
{/foreach}
</ul>

 

Notice that in cms controller

/* List images */
 $this->context->smarty->assign('images', $azgallery->getImages( 1 ));

I manually set id to 1 for first album.

 

Also you should try to contact module developer, maybe he could be of better help.

  • Like 1
Link to comment
Share on other sites

  • 7 months later...
  • 1 year later...
×
×
  • Create New...