Basically every week I have to reverse the following snippet
<!-- Homepage Slider Begin -->
<div class="container-fluid">
<div class="single-item-home hidden-xs">
<div class="slide slide--has-caption">
<a href="/1">
<img src="/sliders/1_example.jpg">
</a>
</div>
<div class="slide slide--has-caption">
<a href="/2">
<img src="/sliders/2_example.jpg">
</a>
</div>
<div class="slide slide--has-caption">
<a href="/3">
<img src="/sliders/3_example.jpg">
</a>
</div>
<div class="slide slide--has-caption">
<a href="/4">
<img src="/sliders/4_example.jpg">
</a>
</div>
</div>
</div>
<!-- Homepage Slider End -->
Basically I'm wanting to make awk script and have a cron job to essentially take lines 4-8 to swap with lines 22-26 and lines 10-14 swap with lines 16-20 however I can only seem to find a way to swap one line and not line blocks.
Is this even possible with awk or just silly?
You may use awk . Below script
awk 'NR==FNR{line[i++]=$0}
END{
for(j=0;j<i;j++){
if(j>=3 && j<=7){
print line[j+18];
continue;
}
else if(j>=21 && j<=25){
print line[j-18];
continue;
}
else if(j>=9 && j<=13){
print line[j+6];
continue;
}
else if(j>=15 && j<=19){
print line[j-6];
continue;
}
print line[j];
}
}' file
will do what you want.
Sample Output
<!-- Homepage Slider Begin -->
<div class="container-fluid">
<div class="single-item-home hidden-xs">
<div class="slide slide--has-caption">
<a href="/4">
<img src="/sliders/4_example.jpg">
</a>
</div>
<div class="slide slide--has-caption">
<a href="/3">
<img src="/sliders/3_example.jpg">
</a>
</div>
<div class="slide slide--has-caption">
<a href="/2">
<img src="/sliders/2_example.jpg">
</a>
</div>
<div class="slide slide--has-caption">
<a href="/1">
<img src="/sliders/1_example.jpg">
</a>
</div>
</div>
</div>
<!-- Homepage Slider End -->
Note: I leave the array-bounds check up to you. If the content of the file is static, you may not need this
This doesn't care how many lines are in each block or where they start/end in the file and it doesn't require you to store the whole file in memory (though most of the file is the "slides" which DO need to be stored so that's probably a non-issue):
$ cat tst.awk
/<div class="slide/ { inSlide=1; slide="" }
inSlide {
slide = slide $0 ORS
if ( /<\/div>/ ) {
slides[++numSlides] = slide
inSlide = 0
}
next
}
/<\/div>/ {
for (slideNr=numSlides; slideNr>=1; slideNr--) {
printf "%s", slides[slideNr]
}
numSlides = 0
}
NF
.
$ awk -f tst.awk file
<!-- Homepage Slider Begin -->
<div class="container-fluid">
<div class="single-item-home hidden-xs">
<div class="slide slide--has-caption">
<a href="/4">
<img src="/sliders/4_example.jpg">
</a>
</div>
<div class="slide slide--has-caption">
<a href="/3">
<img src="/sliders/3_example.jpg">
</a>
</div>
<div class="slide slide--has-caption">
<a href="/2">
<img src="/sliders/2_example.jpg">
</a>
</div>
<div class="slide slide--has-caption">
<a href="/1">
<img src="/sliders/1_example.jpg">
</a>
</div>
</div>
</div>
<!-- Homepage Slider End -->
perl -e '#f=<>; print #f[0..2,21..25,8,15..19,14,9..13,20,3..7,26..$#f]' ip.html
-e option to pass Perl code from command line itself
#f=<> Reads the contents of file (passed as command line argument) into an array
and then print as per order required (index starts from 0, $#f gives last index of array #f)
This is a solution, where you define an order where to print in the BEGIN section and in that order it will print:
$ cat > preordered.awk
BEGIN {
split("1,2,3,22,23,24,25,26,9,16,17,18,19,20,15,10,11,12,13,14,21,4,5,6,7,8",a,",")
}
{
b[(NR in a?a[NR]:NR)]=$0
}
END {
PROCINFO["sorted_in"]="#ind_num_asc"
for(i in b)
print b[i]
}
Give it a go:
$ awk -f preordered.awk' file
<!-- Homepage Slider Begin -->
<div class="container-fluid">
<div class="single-item-home hidden-xs">
<div class="slide slide--has-caption">
<a href="/4">
<img src="/sliders/4_example.jpg">
</a>
</div>
...
I have a blade template that looks something like this:
<div class="comment">
<div class="username"></div>
<div class="message"></div>
<div class="children">
</div>
</div>
I need to be able to call this view from a function and insert data into it.
I have a class, Helpers.php, with a recursive function that looks like this:
function getComments(array $comments)
{
foreach ($comments as $comment)
{
echo $comment;
if (!empty($comment->children))
{
getComments($comment->children);
}
}
}
What this is meant to do is print out a comment, and then check if that comment has any children comments and recurse if it does.
How can I modify my view/function so that I can send data to the view, such that I end up with something like this:
<div class="comment">
<div class="username">Username1</div>
<div class="message">Hello, world!</div>
<div class="children">
<div class="comment">
<div class="username">Username1</div>
<div class="message">Hello, world!</div>
<div class="children">
<div class="comment">
<div class="username">Username1</div>
<div class="message">Hello, world!</div>
<div class="children">
</div>
</div>
</div>
</div>
</div>
</div>
<div class="comment">
<div class="username">Username1</div>
<div class="message">Hello, world!</div>
<div class="children">
</div>
</div>
Thanks!
You don't have to use a function in this particular case.
To achieve your goal, create a view comments.blade.php. Depending on your Laravel version and setup you can put it into app/views or resources/views directory.
The content of this file has to be like follows:
#foreach ($comments as $comment)
<div class="comment">
<div class="username">{{ $comment->username }}</div>
<div class="message">{{ $comment->message }}</div>
<div class="children">
#include('comments', ['comments' => $comment->children])
</div>
</div>
#endforeach
Then render this view and pass the same argument you were passing to your getComments() function.
This will hopefully give the desired result.
Good luck!
you can send the data to view from function you can use by three method compact,with and session. compact of with method while your are redirecting of loading any view you can use compact or ->with(array());
i move breadcrumbs, but now i need that Title Page show below, actually html show
<div class="breadcrumbs"></div>
<div class="col-main">
<div class="my-account">
<div class="page-title"></div>
</div>
</div>
I need
<div class="breadcrumbs"></div>
<div class="page-title"></div>
<div class="col-main">
<div class="my-account"></div>
</div>
How can i do this, i suposse is necessary modify xml file but i not find the solution.
Thanks
Well, I choose create new custom block in page.xml
<block type="core/template" name="my_top_title" as="my_top_title" template="page/html/toptitle.phtml"/>
In 2columns-left.phtml,
<?php echo $this->getChildHtml('my_top_title') ?>
In toptitle.phtml
<?php $title=$this->getLayout()->getBlock('head')->getTitle(); ?>
<div class="my-title">
<h1><?php echo $title ?></h1>
</div>
Works fine.
I'm trying to add some smarty to my template , to first element i want to give
<div class="item active">
and for next elements
<div class="item">
I tried to do something like this:
{foreach $imageCollection as $image key=slider}
{if $slider < 1}
<div class="item active">
<img class="img-responsive" src="{$image->getPath('1400x469')}" alt="caption1" />
<div class="carousel-caption">
{else}
<div class="item">
<img class="img-responsive" src="{$image->getPath('1400x469')}" alt="caption1" />
<div class="carousel-caption">
{/if}
{/foreach}
It's doesn't work, all elements are displaying with
<div class="item active">
use #first:
{foreach $imageCollection as $image}
<div class="item {if $image#first}active{/if}">
...
{/foreach}
The correct syntax for getting the index key in Smarty is as following:
{foreach from=$imageCollection item=image key=slider}
// Your code
{/foreach}
My products in Magento have attribute Brand. What I need to do is to display a list of Brands in the footer.Something like: Our Brands: Brand 1, Brand 2, Brand 3...
As far as I understand I need somehow retrieve values from Advanced search and display them in footer as a list, but I don't know how to do it. Does anybody have solution for this?
There are several steps to follow
here i am giving detail instruction how to add your custom attribute at footer.
1. you have to create on block to get all your brand product with assign your custom attribute
for block.
$attributes = Mage::getSingleton('eav/config')
->getEntityType(Mage_Catalog_Model_Product::ENTITY) // pass your attribute id
->getAttributeCollection()
->addSetInfo();
foreach ($attributes as $attribute)
{
if ($attribute->usesSource())
{
echo "{$attribute->getFrontendLabel()}:\n";
foreach ($attribute->getSource()->getAllOptions() as $option)
{
echo " {$option['label']}\n";
}
echo "\n";
}
}
above is print logic you should have to store it one array in return with one variable.
2. create view file in your theme for display purpose and call that block function in that home_logo file.
<?php $_brandsCollection = $this->getBrandsLogoCollection();?>
<div class="block block-layered-nav">
<div class="block-title">
<strong><span><?php echo $this->__('Brands') ?></span></strong>
</div>
<div class="block-content" >
<div id="Carousel2" class="carousel">
<div class="button navButton previous" style="display:none;">Back</div>
<div class="button navButton next" style="display:none;">More</div>
<div class="container">
<div class="items">
<?php foreach ($_brandsCollection as $_brand): ?>
<div class="item">
<div class="key caption"></div>
<div class="icon">
<img class="brand-base-logo" alt="<?php echo $_brand->getBrandLogo() ?>" src="<?php echo $_brand->getBrandLogo(); ?>" width="50" height="50">
</div>
<div class="picture">
</div>
</div>
<?php endforeach; ?>
</div>
</div>
</div>
</div> <!-- end block content-->
</div>
3. Assign that file to footer using your_layout.xml with reference before footer.
<reference name="footer">
<block type="brand/left" name="brands_logolist" before="-" template="brand/home_logo.phtml" />
</reference>
Hope you can understand my logic.