I am trying to use a View composer to automatically modify page titles based on a defined section.
#section('title', 'Page')
And use it like this:
<title>{{ $title }}</title>
I've written the following code as the composer, but it doesn't work correctly. It just displays the title as Website - Website when it should say Page - Website or, in the case of not including a title, Website.
View::composer('*', function($view){
$title = $view->title;
$view->with('title', !empty($title) ? $title . " - Website" : "Website");
});
This only needs to work on one template, master, but when I replaced '*' with 'master' it didn't modify the behaviour at all.
What do I need to change to make this correctly modify the title section?
You can use
view()->composer('*', function ($view) {
$view->with('title', 'Your title');
});
In service provider.
Or you can use share
view()->share('key', 'value');
Then in view use
{{ $key }}
May be try to use this:
view()->composer('*', function ($view) {
$data = $view->getData();
//here you can get your data which was sent to view
//exmp. $old_title = $data['title'];
//where 'title' - key of variable which sent via ->with('title','data')
$view->with('title', 'new title');
});
Related
I would like to have an h1 tag that displays the current file name.
For example, in the blade file "index.blade.php", I would like to have an h1 tag that displays "index.blade.php".
I have tried doing using {{__FILE__}} but that prints out the cached blade file and not the actual name
// index.blade.php
<h1>{{__FILE__}}</h1>
Result:
project/storage/framework/views/eweijo29398hr23.php
Desired Result: /index.blade.php
Here is still working solution:
How can I get the current view name inside a master layour in Laravel 4?
View::composer('*', function($view){
View::share('view_name', $view->getName());
});
#php
app('events')->listen('composing:*', function ($view, $data = []) {
echo last(explode('/', $data[0]->getEngine()->getCompiler()->getPath()));
});
#endphp
It's slightly messy but works!
You may find me stupid but i am unable to understand on what basics we give url and name in our route file
Example:
Route::get('/order/getOrders', 'OrderController#getOrders')-
>name('order.getOrders')->middleware('auth');
can anyone please tell me.
and if we take url on the basics of where our file in view folder like( order->getorder blade file)
Then what if my path is layouts.site.topbar
In view instead of pages, my file is in layouts.
EDIT:
blade file
<a href="{{ route('sync.index') }}">
#if(isset($syncs))
#foreach ($syncs as $sync)
#endforeach
{{ $sync->session_date }}
#endif
</a>
controller file
class TopbarController extends Controller
{
public function index()
{ die('o');
$syncNames = Sync::select('session_date','session_time')->where('user_id',$user_id)->get();
return view('layouts.site.topbar', array(
'syncs' =>$syncNames
));
}
public function sync_finish_session() {
die('s');
$user_id = Auth::id();
$sync_date = date('M d ',strtotime("now"));
$sync_time = date('M d, Y H:i:s',strtotime("now"));
$sync = Sync::where('user_id',$user_id)->get();
if(count( $sync) > 0) {
Sync::where('user_id',$user_id)->update(['session_date'=>$sync_date,'session_time'=>$sync_time,'user_id'=>$user_id]);
}
else {
$dates = new Sync();
$dates->session_date = $sync_date;
$dates->session_time = $sync_time;
$dates->user_id = $user_id;
$dates->save();
}
return $sync;
}
}
web file
Route::post('/sync_finish_session', 'TopbarController#sync_finish_session')->name('sync_finish_session')->middleware('auth');
Route::get('/sync/index', 'TopbarController#index')->name('sync.index')->middleware('auth');
Now whats the problem its giving nothing even i put die but its not going in controller file.
I think this is more a personal preference thing than that there are rules.
The convention I use is name(<model>.<action>)
This way i can create routes like
Route::get('/users/{id}/view', 'UserController#view')->name('users.specific.view')->middleware('auth');
You just name route as you do want. There is no strict rules how to name route. You can change name('order.getOrders') to name("anyName") and use new name in templates.
As the Laravel documentaton about rounting says:
Named routes allow the convenient generation of URLs or redirects for specific routes.
So, you can use this name to generate URLs or redirects. For example:
You could put this in your web.php file:
Route::get('/image/index', 'API\SettingsController#index')->name('image.index');
And call that route like this in your view:
Le met see that index!
Where the {{ route('image.index') }} references the name you gave to it.
You can name your route(s) anything you want. If you wanted, you could call your above route "mySuperCoolRouteName":
Route::get('/order/getOrders', 'OrderController#getOrders')-
>name('mySuperCoolRouteName')->middleware('auth');
and later in a view file you can use this name as a "shorthand" to get/print the URL of that route:
To My Cool Route
will be rendered to
To My Cool Route
Is it possible to set a series of global properties (such as social media usernames) that are available to all page views in OctoberCMS rather than having them associated to one CMS page or Static Page at a time?
For example, being able to use {{ twitter_username }} in any template, but it wouldn't show up as a field in any page form on the backend.
UPDATE: this can be achieved by registering a Twig function using registerMarkupTags in your plugin:
use System\Classes\PluginBase;
class Plugin extends PluginBase
{
public function registerMarkupTags()
{
return [
'functions' => [
'globals' => function($var) {
switch ($var) {
case 'twitter_username':
return 'mytwitterusername';
}
return null;
},
],
];
}
}
In this case, calling {{ globals('twitter_username') }} from any template prints mytwitterusername.
Hmm yes better you need to add code to life-cycle method in layouts, so now page which are using that layout will have this info already loaded.
In layout code block you can use something like this
use RainLab\Pages\Classes\Page as StaticPage;
function onStart() {
$pageName = 'static-test'; // this will be static page name/filename/title
$staticPage = StaticPage::load($this->controller->getTheme(), $pageName);
$this['my_title'] = $staticPage->viewBag['title'];
$this['twitter_username'] = $staticPage->viewBag['twitter_username'];
}
now inside your cms page you can use this variable
<h1>{{ my_title }} </h1>
<h3>{{ twitter_username }} </h3>
let me know if it you find any issues
You could also use theme config file which gives you more flexibility rather than hardcoding the values in to the code block.
https://octobercms.com/docs/themes/development#customization
I am trying to implement dynamic meta tags in my laravel app. I have a head.blade.php file in which i have all the code that goes in head. This is what i have been doing, i have created a file called meta-conf.php under pages folder, the head.blade.php contains -
#if(Route::currentRouteName() != false)
{{
$page = Route::currentRouteName();
}}
#include('pages.meta-conf');
{{
$title = $meta[$page]['title'];
$keywords = $meta[$page]['keywords'];
$description = $meta[$page]['description'];
}}
#else
{{
$title = "akademe";
$keywords = "";
$description = "Quickly find near by courses";
}}
#endif
The meta-conf.php contains -
<?php
$meta['index']['title'] = "Home";
$meta['index']['keywords'] = "explore, learn, repeat, education, register, institute, courses";
$meta['index']['description'] = "Quickly find near by courses";
Now its throwing me an error "Undefined variable: meta ". I have included the meta-conf.php file which contains the $meta array defined as you can see above. Dunno what is causing this issue.
What I would do instead of what you're doing is this:
Add the title, keywords and description as public variables to your base Controller (app/Http/Controllers/Controller.php)
Initialise the variables on the Controller constructor and set the "default" values
From your controllers (Courses, Search, etc.) set the specific values for each page
Implement a "renderPage" method on your base Controller so that you can pass a view and parameters from your controllers
Render method will load the requested view and pass the parameters and your meta tag variables
Update your head.blade.php to use your meta variables
I haven't tested any of the above but thinking something like that should work :)
I have create.blade.php view modal and i want it to be used with and without default content.
example:
<div class="form-group">
{{ Form::text('title', $content->title, array('class' => 'form-control' , 'placeholder' => 'Insert Title Here.')) }}
</div>
The $content obj doesn't always exists so i get error like this (when there is no content set to $content):
Trying to get property of non-object
This is my controller function:
public function create($default_content = '')
{
return View::make('content.create')
->with('content', $default_content);
}
I tried to set default/fake obj:
$default_content = ($default_content == '') ? new stdClass() :
$default_content;
But in the end i get error that $content->title dose not exists.
Should i set all the variables to NULL in the obj if $default_content is empty ? if so, how?
There must be a better way to handle this problem - Thanks!
There are a few things you could probably do.
I'm assuming since you are building a form, content is actually a model, in which case when you open the form, use Form::model($content), then Laravel will automatically set those values for you.
You can read more about that here... http://laravel.com/docs/html#form-model-binding
If content is not a model, you could setup a view composer, which will automatically inject $content into your view each time it's loaded.
View::composer('content', function($view)
{
// Retrieve your content
// Inject the content into the view each time it's loaded.
$view->with('content', $content);
});
Can check http://laravel.com/docs/responses#view-composers for more info
The last solution would be to check for it in your view before setting it though usually, it's a good idea to keep this kind of logic out of your views.
{{ Form::text('title', isset($content->title) ? $content->title : '', array('class' => 'form-control' , 'placeholder' => 'Insert Title Here.')) }}