1

Really new in Laravel and frameworks at all so please bear with me on this. I'm trying to learn it and I choose to write simple index page with few products ( title, price, image, description ). So far everything is there. Now I'm trying to put ViewMore button on the description section and when I click on button to load me new page with more information for this product.

This is the view of index page where I show all products and want to make button view more...

@if($product['image'])
    <img class="max-150" src="{{ $product['image'] }}" alt="{{{ $product['title'] }}}" />
     <br /><br />
@endif
@if($product['description_small'])
     <p>{{ $product['description_small'] }}</p>
     <p>{{ str_limit($product['description_small'], $limit = 250, $end = '<br><br> <a href="{{ URL::to('/product/single/' . $product['product_id']) }}" class="btn btn-primary">View More</a>') }}</p> 
@endif

For now I've got error in link of View More and the error says

production.ERROR: exception 'Symfony\Component\Debug\Exception\FatalErrorException' with message 'syntax error, unexpected 'product_id' (T_STRING)' in....

When I make it only with a href it's working great...

 <p><br><br> <a href="{{ URL::to('/product/single/' . $product['product_id']) }}" class="btn btn-primary">View More</a></p>
S.I.
  • 3,250
  • 12
  • 48
  • 77
  • I don't think the syntax error is triggered in this piece of code you've added. On the side: why is product an array? Use Eloquent :) Edit: I spotted the syntax error, answering in a bit – JasonK May 30 '16 at 16:20
  • It's from this.. when the link is like: `{{ str_limit($product['description_small'], $limit = 250, $end = '

    View More') }}` it throws this error.. but if I make it like this `



    View More

    ` there is no problems but I don't get description
    – S.I. May 30 '16 at 16:22

1 Answers1

1

You were echo'ing within an echo ({{ {{ }} }}).

This works:

{{ str_limit($product['description_small'], $limit = 250, $end = '<br><br><a href="' . URL::to('/product/single/' . $product['product_id']) . '" class="btn btn-primary">View More</a>') }}

Though I find this piece of code a bit sloppy and hard to read, a cleaner approach would be:

$button = sprintf('<a href="%s" class="btn btn-primary view-more">View More</a>', URL::to('product/single/' . $product['product_id']));

echo str_limit($product['description_small'], 250, $button);

... or something like (which I like the most):

{{ str_limit($product->description, 250) }}

@if (strlen($product->description) > 250)
    <a href="{{ URL::to('product/single/' . $product->id) }} " class="btn btn-primary view-more">View More</a>
@endif

Don't use those nasty inline <br> tags, styling .btn.view-more with some CSS will do the trick :)

JasonK
  • 5,214
  • 9
  • 33
  • 61
  • Thank's for the answer. It's really helped me! – S.I. May 30 '16 at 16:44
  • I've implemented your most preferred way. I just have a question. Is it possible to not cut words like this? :) – S.I. May 30 '16 at 16:58
  • 1
    Yes, check this question http://stackoverflow.com/questions/3538130/how-to-capture-complete-words-using-substr-in-php-limit-by-word – JasonK May 30 '16 at 17:14