Using PHP template engine on Laravel blade - php

im calling v-for event on div tag to create <a> tag by every object on results array
Code attempt:
results array:
html (Laravel blade):
#verbatim
<div class="category-item" v-for="result in results" :value="result.id">
<a class="button-product-info-s" href="/product/{{ result.id }}"/>{{ result.name }}</a>
</div>
#endverbatim
brower output:
as you can see {{ result.name }}} is just working and {{ result.id }} is not outputing anything
i tried using .{{ result.id }}. but didn't work

Have you tried changing your href attribute to :href="'/product/'.result.id"?
Or with the + operator instead of .

Change your code to something like:
#verbatim
<div class="category-item" v-for="result in results" :value="result.id">
<a class="button-product-info-s" href="/product/#{{ result.id }}"/>#{{ result.name }}</a>
</div>
#endverbatim

Related

How to insert a variable into a Laravel Blade Template that is inside a HTML href tag for a web.root link

I am trying to creating a blog post with Laravel using views (blog.blade.php) inside HTML.
The code in my laravel web.php fetches the blog posts from my local mssql server looks as follow (and works):
Route::get('/blog/{id}', function($id){
$posts = DB::table('blog_posts')->get();
return view('blog', ['id'=>$id], ['posts'=>$posts]); });
In the laravel views dir home.blade.php my code works with hardcoded route: url('/blog/1')
The href link takes you to the full blog post
<h2>Blog posts:</h2>
#foreach ($posts as $post)
<p>{{ $post->title }} </p> <a href="{{ url('/blog/1') }}" >▲ Click here to view full post</a>
#endforeach
What I am trying to do is:
<p>{{ $post->title }} </p> <a href="{{ url('/blog/{{ $post->id }}') }}" >▲ Click here to view full post</a>
How do I get the $post->id that is the route (1, 2, 3 or 4) and comes from the sql data base saved to the blog row to work. I get the following error returned from my xampp php server when using {{ $post->id }} instead of hardcoding "1" .
D:\XAMPP Server\htdocs\cool-blog
ParseError
Unclosed '(' does not match '}' (View: D:\XAMPP Server\htdocs\cool-blog\resources\views\home.blade.php)
http://localhost:8000/
Pass second param to url() method
<p>{{ $post->title }} </p> <a href="{{ url('blog',$post->id) }}" >▲ Click here to view full post</a>
Ref:https://laravel.com/docs/8.x/helpers#method-url
Try this
<p>{{ $post->title }} </p> <a href="{{ url('/blog/'.$post->id.'') }}" >▲ Click here to view full post</a>

Display selected elements twig template

data= [{id:1, text:'abc'},{id:2, text:'cde'}]
In my template I want to do this:
<ul>
{{% for article in data %}}
<li><a href={{article.id}}>{{article.id}}</a></li>
</ul>
In click on li I would display only the property "text" of related id.
<div>{{selected article.text}}</div>
If clicked 1 I want to see "abc".
Is there a way to do it with twig?
If I understand correctly, you cant to show dynamically the text related to an id on click on the li containing the related id.
This cannot be achieved with only twig. You need some javascript here. Basically what you can do is the following:
<ul>
{% for article in data %}
<li>
<a href="#" data-content="{{ article.text|e('html_attr') }}" onclick="document.getElementById('dynamic-content').innerHTML = this.dataset.content; return false">
{{ article.id }}
</a>
</li>
{% endfor %}
</ul>
<div id="dynamic-content"></div>
|e('html') is some escaping for keeping it inside an html attribute, learn more here
onclick="" this contains pure javascript, it work like this but I recommand to do it another way (checkout this JSFiddle)

Pagination Link

What is the correct way to call the "Links" function after this "Foreach"?
I don't know how to handle the variable to put in function.
#inject('usuarios', 'App\User')
#foreach($usuarios->getIndicados() as $user)
#endforeach
<div class="row">
<div class="col-12 text-center">
{{ $usuarios->getIndicados()->links() }}
</div>
</div>
Maybe it's just an editing error, but in your output the -tags don't seem to be closed again. Also, there should be no space like < a> at the beginning of the tag. And < a hr_ef= ... is obviously wrong.
In order to style them, you can add a class attribute to the tags while building the string and do the style-stuff in css.
This is what laravel document provides. You need to add links in the collection.
<div class="container">
#foreach ($users as $user)
{{ $user->name }}
#endforeach
{{ $users->links() }}

Included blade template repeating in each foreach loop iteration

I'm using Blade templating with Laravel and I'm trying to use a #foreach loop to display notifications. The problem is that if I have say 10 notifications, the first notification is repeated 10 times.
The code to output each notification:
#foreach ( Auth::user()->unreadNotifications as $notification )
{{ $notification->type->web_template }}
{{ $notification->id }}
#include($notification->type->web_template)
#endforeach
web_template will output a path to the template: notifications.web.user_alert
For each iteration of the loop the {{ $notification->type->web_template }} and {{ $notification->id }} will output what they're supposed to but #include($notification->type->web_template) will only output the first notification each time.
So the output will look like:
156 notification.web.new_message
You have a new message from Joe.
154 notification.web.user_alert
You have a new message from Joe.
145 notification.web.new_like
You have a new message from Joe.
I think it's some sort of cache issue maybe, but I couldn't find anyone with the same problem.
Any ideas?
UPDATE: Adding some code
Example notification view:
#extends('layouts.notification-wrapper')
#section('url', url('jobs/'.$notification->job_id.'#highlight'.$notification->bid_id))
#section('image', '/assets/img/no-photo.jpg')
#section('header', $notification->collector->firstname . ' ' . $notification->collector->secondname)
#section('description')
has placed a bid of €{{ number_format($notification->bid()->withTrashed()->first()->amount,0) }} on your job.
#stop
Notification wrapper:
<li #if(!$notification->read)
class="unread"
#endif>
<a href="#yield('url')" data-id="{{ $notification->id }}">
<div class="pull-left">
<img src="#yield('image')" class="img-circle" alt="user image">
</div>
<h4>
#yield('header')
<small><i class="fa fa-clock-o"></i> {{ $notification->created_at->diffForHumans()}}</small>
</h4>
<p> #yield('description')</p>
</a>
</li>
Answering my own question!
Found this: Laravel Blade Templates Section Repeated / cache error
Basically whatever way it works I need to overwrite my sections when looping and using #yield... I think. So I need to replace #stop with #overwrite in my views.

Twig/Symfony not rendering form_widget

I have a twig template (home.twig), and I'm using
{{ render(controller('WebsiteUserBundle:Registration:register',{ 'template': 'popup'} )) }}
inside that template to render another template (login.twig), which is used to login the user from a popup.
The problem is that the form_widget isn't rendered, but the form_label is.
This is part of my login template:
<div class="row-fluid">
{{ form_label(form.email, 'Email:')}}
{{ form_widget(form.email, { 'attr': {'class': 'span12'} }) }}
</div>
And by "it's not rendered", I mean that there isn't even an empty div or input next to the label element in the DOM.
Why is this happening?
I had the same issue and was because I had the follow sentence before the form_widget
{{ form_rest(form) }}
Check that the form_rest be after of all your form_widget.
if you get only form_widget without extra params, like this :
{{ form_widget(form.email) }}
do you have more informations or any output ?

Categories