I have a array of images in order:
1-3 are main images.
3+ are thumbnails.
Q) How can I split the thumbs on 2 lines.
E.g:
<div class="main-images">
{section name="i" loop=$images.rows}
{assign var="rows" value=$images.rows[i]}
{if $smarty.section.i.index <= 2}
<img />
{/if}
{/section}
</div>
<div class="thumbs-images">
{math assign=thumbs_count equation="total - other" total=$images.rowcount other=3}
{section name="i" loop=$images.rows}
{assign var="rows" value=$images.rows[i]}
{if $smarty.section.i.index >= 3}
<img />
{/if}
{/section}
</div>
So I need to add something {if $thumbs_count >= $smarty.section.i.index}<div style="clear:both" />{/if} so this appears half way through the second loop.
Managed to figure this out.
{math assign="total_row_split" equation="floor((total - main) / division)" total=$images.rows|#count division=2 main=3}
{counter start=0 print=false assign="thumbs_count"}
{section name="i" loop=$images.rows}
{assign var="rows" value=$images.rows[i]}
{if $smarty.section.i.index >= 3}
{if $thumbs_count == $total_row_split}<br style="clear:both" />{/if}
<a href="{$HOME}/get/image{$rows.filename.fvalue}" rel="fancy" title="{$rows.title.value|default:$product.name.fvalue}">
<img src="{$HOME}/get/image/120{$rows.filename.fvalue}" alt="{$rows.title.value|default:$product.name.fvalue} Picture" />
</a>
{counter print=false}
{/if}
{/section}
Related
I'm attempting to create a navigation while using smarty for the first time. I want to display all categories except one which I want to capture and use later on.
I got it to work and get the categories and put them in list. However that one category which is being captured, does not display where I want it to display.
my categories
Soccer //
subCat1 // those categories should be captured
subCat2 //
subCat3 //
Rugby
subCat1
subCat2
subCat3
Netball
subCat1
subCat2
subCat3
etc...
so my code goes like this:
<div>
<!-- deal with category levels -->
{if empty($category_level)}
{assign var="category_level" value=1}
{else}
{math equation="x + 1" x=$category_level assign="category_level"}
{/if}
{assign var="captured" value="false"}
{foreach from=$categories item="category"}
{if (strstr($category.name, 'Soccer') == true)}
{assign var="captured" value="y"}
{assign var="capItem" value="soccerCats"} <!-- used it to see whether the condition was met and this code has been run -- this has been assigned corerctly -->
{capture name="soccerCats" assign="soco"} <!-- start capturing if above condition was met -->
{/if}
{if $category.level == $category_level && $category.is_visible == "Yes"}
<ul>
<li class="{$category.name|htmlspecialchars}Cat">{$category.name|htmlspecialchars}<a class="mobileOnly"><i class="fa fa-angle-down fa-fw"></i></a>
{if !empty($category.children)}
<ul>
{assign var="categories" value=$category.children}
{if empty($category_level)}
{assign var="category_level" value=1}
{else}
{math equation="x + 1" x=$category_level assign="category_level"}
{/if}
{foreach from=$categories item="category"}
{if $category.level == $category_level && $category.is_visible == "Yes"}
<li>{$category.name|htmlspecialchars}</li>
{/if}
{/foreach}
{math equation="x - 1" x=$category_level assign="category_level"}
{assign var="category" value=0}
</ul>
{/if}
</li>
</ul>
{/if}
{if $captured == "y"}
{/capture} <!-- end the capture -->
{assign var="captured" value="n"}
{/if}
{/foreach}
{math equation="x - 1" x=$category_level assign="category_level"}
{assign var="category" value=0}
</div>
<!-- I tried to use both ways shown below to display the captured item -->
{$soco}
{$smarty.capture.soccerCats}
any idea why is this happening?
I don't think {capture} works inside {if} tags, and if does, I'd advise against it, for better code readability. Try something like this:
<div>
<!-- deal with category levels -->
{if empty($category_level)}
{assign var="category_level" value=1}
{else}
{math equation="x + 1" x=$category_level assign="category_level"}
{/if}
{foreach from=$categories item="category"}
{capture name="code"}
<ul>
<li class="{$category.name|htmlspecialchars}Cat">{$category.name|htmlspecialchars}<a class="mobileOnly"><i class="fa fa-angle-down fa-fw"></i></a>
{if !empty($category.children)}
<ul>
{assign var="categories" value=$category.children}
{if empty($category_level)}
{assign var="category_level" value=1}
{else}
{math equation="x + 1" x=$category_level assign="category_level"}
{/if}
{foreach from=$categories item="category"}
{if $category.level == $category_level && $category.is_visible == "Yes"}
<li>{$category.name|htmlspecialchars}</li>
{/if}
{/foreach}
{math equation="x - 1" x=$category_level assign="category_level"}
{assign var="category" value=0}
</ul>
{/if}
</li>
</ul>
{/capture}
{if (strstr($category.name, 'Soccer') != true)}
{$smarty.capture.code}
{else}
{$soccercats=$smarty.capture.code} {*Updated with #Borgtex's comment*}
{/if}
{/foreach}
</div>
{$soccercats}
The idea is that you capture everything and if the category is not 'soccer', you display the captured data; otherwise, you assign it to another capture, to use it in the end, outside the loop. The code assumes that there is a single soccer category, as you initially stated in your question.
I am using the even and odd logic for that but not getting the output as i need
for example i have array of
$data = array(1000,1001,1002, 1003,1004,1005);
$smarty->assign('data',$data);
{section name=i loop=$data}
{section}
so the output i need is :
<div>
<dl>1000</dl>
<dl>1001</dl>
</div>
<div>
<dl>1002</dl>
<dl>1003</dl>
</div>
<div>
<dl>1004</dl>
<dl>1005</dl>
</div>
According to offical document http://www.smarty.net/docsv2/en/language.function.section.tpl
Try following codes:
<div>
{section name=i loop=$data}
<dl>{$data[i]}</dl>
{if $smarty.section.data.index > 0 && $smarty.section.data.index % 2 == 0 && $smarty.section.data.index < $smarty.section.customer.total -1}
</div><div>
{/if}
{/section}
</div>
You should do it this way:
{section name=i loop=$data}
{if $smarty.section.i.index %2 == 0}
<div>
{/if}
<dl>{$data[i]}</dl>
{if $smarty.section.i.index %2 == 1 || $smarty.section.i.last}
</div>
{/if}
{/section}
<div> should be rather created in section because when there weren't be any items you probably wouldn't like to create empty <div>. Last condition $smarty.section.i.last is added in case you have for example 5 elements and in that case you of course make sure your div is closed.
Need to delete first element after all this statements, i try to add key=i to second foreach, and add {if $i!=0}, but it dont work for me, because i have multilevel system and after all if statements first $i is different for all pages.
{foreach from=$page.path item=e}
{assign var=element value=0}
{foreach from=$menu3 item=r}
{if $e.page_id==$r.page_id}{assign var=element value=$r}{/if}
{if $element._left < $r._left && $element._right > $r._right}
{if $r._level==$element._level+1 && $r._level==$page._level}
{$r.name}
{/if}
{/if}
{/foreach}
{/foreach}
P.S. Sorry for my English.
Add a name to you foreach and do something like this to get index
{foreach from=$items key=myId item=i name=foo}
{$smarty.foreach.foo.index} <!-- It will show index -->
{/foreach}
I assume you don't want to display first element in each of the menus you have, right?
{foreach from=$page.path item=e}
{assign var=first_skipped value=0}
{assign var=element value=0}
{foreach from=$menu3 item=r}
{if !$first_skipped}
{assign var=first_skipped value=1}
{else}
{if $e.page_id==$r.page_id}{assign var=element value=$r}{/if}
{if $element._left < $r._left && $element._right > $r._right}
{if $r._level==$element._level+1 && $r._level==$page._level}
{$r.name}
{/if}
{/if}
{/if}
{/foreach}
{/foreach}
I'm learning PHP "on the go" as I edit a template for a website which I have to do at work, and I'm kinda lost. The original code looks like this:
<!-- start of top menubar -->
<table border="0" cellpadding="0" cellspacing="0" width="{$web_var_main_width}" align="center">
<tr style="background-color:#{$web_var_top_tabs_background};">
{php}
global $lang;
$this->assign('web_var_top_folder', $this->get_template_vars('web_var_top_folder_'.$lang));
{/php}
<td align="right" style="{if $web_var_top_tabs_image ne "" and $web_var_top_tabs_image ne 0}background:url(http://{$lang}.{$website_name}/{$RSwebPath}utilities/getScaledPicture.php?imageID={$web_var_top_tabs_image}&w={$web_var_top_tabs_image_x}&h={$web_var_top_tabs_image_y}&adj=d) top left repeat-y;{/if}background-color:#{$web_var_top_tabs_background_right};">
{if $folders_structure|#count gt 0}
{if $web_var_top_folder gt 0}
{section name=findtop loop=$folders_structure}
{if $folders_structure[findtop].key eq $web_var_top_folder}{assign var="top_menu" value=$folders_structure[findtop].childs}{/if}
{/section}
{if $top_menu|#count gt 0}
<table border="0" cellpadding="0" cellspacing="0" align="right">
<tr>
{if $web_var_top_font_size eq "small"}
{assign var="web_var_top_font_size" value="10px"}
{elseif $web_var_top_font_size eq "medium"}
{assign var="web_var_top_font_size" value="12px"}
{elseif $web_var_top_font_size eq "large"}
{assign var="web_var_top_font_size" value="14px"}
{elseif $web_var_top_font_size eq "extra large"}
{assign var="web_var_top_font_size" value="16px"}
{/if}
{section name=mytopsec loop=$top_menu}
<td width="5"></td>
{strip}
{if $top_menu[mytopsec].key eq $folders_selected[1]}
{assign var="topsel" value="1"}
<td bgcolor="#{$web_var_top_selected_tabs_color}" style="padding:6px 30px"><span style="font-family:{$web_var_top_font_family}; color:#{$web_var_top_font_color}; font-size:{$web_var_top_font_size}; font-weight:{$web_var_top_font_weight}; font-style:{$web_var_top_font_style}; text-decoration:none">{$top_menu[mytopsec].name}</span></td>
{else}
<td bgcolor="#{$web_var_top_tabs_color}" onMouseOver="this.style.backgroundColor='#{$web_var_top_tabs_hover}'; style.cursor='pointer'; style.cursor='hand'" onMouseOut="this.style.backgroundColor='#{$web_var_top_tabs_color}'" style="padding:6px 30px">{$top_menu[mytopsec].name}</td>
{/if}
{/strip}
{/section}
</tr>
</table>
{/if}
{/if}
{/if}
</td>
</tr>
<!-- end of top menubar -->
I have to send a folder ID which is the one the code scans into and shows all its childs.
And the function which scans the folders and lists them as menu items is this one:
<?
include_once "folderFunctions.php";
global $lang;
$global_deep=0;
$this->assign("folder_structure",getFolderChilds(0,$_SESSION["rs_domain_id"],$lang,"WEB",0,$global_deep));
$this->assign("folder_deep",$global_deep);
?>
Which uses the funcion GetFolderChilds from folderFunctions.php:
function getFolderChilds($identification,$website,$lang,$type,$local_deep,&$global_deep){
$child_list=array();
$childs = #mysql_query("SELECT `HB_IDENTIFICATION`,`HB_TITLE`,`HB_DESCRIPTION`,`HB_IMAGE_IDENTIFICATION`,`HB_LINK` FROM `hb_folders` WHERE `HB_PARENT_IDENTIFICATION` = ".$identification." AND `HB_DELETED`=0 AND `HB_PUBLISHED`=1 AND HB_WEBSITE_ID=".$website.(($type=='image')?(''):(" AND HB_LANGUAGE_ID='".$lang."'"))." AND HB_TYPE='".$type."' ORDER BY HB_ORDER");
$local_deep++;
if($local_deep>$global_deep&&#mysql_num_rows($childs)>0) $global_deep=$local_deep;
while($child = #mysql_fetch_assoc($childs)){
//$auxPermissions=getPermissions($_SESSION["rs_user_login"],$_SESSION["rs_user_pass"],$website,$child['HB_IDENTIFICATION'],'FOLDER');
//if($auxPermissions['read']==1){
$child_list[]=array("key"=>$child['HB_IDENTIFICATION'], "name"=>$child['HB_TITLE'], "desc"=>$child['HB_DESCRIPTION'], "image"=>$child['HB_IMAGE_IDENTIFICATION'], "link"=>$child['HB_LINK'], "childs"=>getFolderChilds($child['HB_IDENTIFICATION'],$website,$lang,$type,$local_deep,$global_deep));
//}
}
return $child_list;
}
And its output is something like this (tab changes color when mouse is on it, and so):
Well, that's what I figured out until now.
The thing is that I want to do a menu with these PHP functions, that is, creating the folders in the server and doing a loop which scans and shows them in my menu.
The thing is, that code uses tables, and has a lot of things which I don't need, such as image tabs and so (I just want to have a text menu with no images).
My question is, how can I implement the function to get the folders and list them not in a table, but in a list, so the output is something similar to this (I want to keep it as simple as posible in order to understand what is being done in any point)?
<header>
<ul class="navigation0">
<li>start</li>
<li>about us</li>
<li>contact</li>
</ul>
</header>
Which shows this:
Thanks a lot.
I believe I've solved it:
{php}
global $lang;
$this->assign('web_var_top_left_folder', $this->get_template_vars('web_var_top_left_folder_'.$lang));
{/php}
{if $folders_structure_left|#count gt 0}
{if $web_var_top_left_folder gt 0}
{section name=findtopleft loop=$folders_structure_left}
{if $folders_structure_left[findtopleft].key eq $web_var_top_left_folder}{assign var="top_left_menu" value=$folders_structure_left[findtopleft].childs}{/if}
{/section}
{if $top_left_menu|#count gt 0}
{if $web_var_top_font_size eq "small"}
{assign var="web_var_top_font_size" value="10px"}
{elseif $web_var_top_font_size eq "medium"}
{assign var="web_var_top_font_size" value="12px"}
{elseif $web_var_top_font_size eq "large"}
{assign var="web_var_top_font_size" value="14px"}
{elseif $web_var_top_font_size eq "extra large"}
{assign var="web_var_top_font_size" value="16px"}
{/if}
<ul class"leftmenu">
{section name=mytopsecleft loop=$top_left_menu}
{strip}
{if $top_left_menu[mytopsecleft].key eq $folders_selected[1]}
{assign var="topselleft" value="1"}
<li><span style="font-family:{$web_var_top_font_family}; color:#{$web_var_top_font_color}; font-size:{$web_var_top_font_size}; font-weight:{$web_var_top_font_weight}; font-style:{$web_var_top_font_style}; text-decoration:none">{$top_left_menu[mytopsecleft].name}</span></li>
{else}
<li onMouseOver="style.cursor='pointer'; style.cursor='hand'>{$top_left_menu[mytopsecleft].name}</li>
{/if}
{/strip}
{/section}
</ul>
{/if}
{/if}
{/if}
<div id="favorite-first" class="">
{foreach from=$arrSection key=k item=v}
{if $k==$selectedSection}
{$v}
{/if}
{/foreach}
</div>
<div id="favorite-toggle"><br></div>
<div id="favorite-inside" class="slideUp">
{foreach from=$arrSection key=k item=v}
{if $k==$selectedSection}
{else}
<div class="favorite-action" id="{$k}">{$v}</div>
{/if}
{/foreach}
</div>
If $arrSection array returns only one value (i.e $k). I need to hide div (favorite-toggle,favorite-inside) How can i do this in smarty
If $arrSection array returns only one value
{if count($arrSection) eq 1}
there is only one item
{else}
there is > one or zero items
{/if}
If you give the "foreach" a name argument you can access certain foreach properties:
{foreach from=$arrSection key=k item=v name=NAME}
Let's say the number of iterations in total:
$smarty.foreach.NAME.total
Then you now how often smarty will loop and if it's in your case only one time.