I am using CakePHP 3.0 and I am using inside the Cell/Header directory a display for the header.
<div id="header">
<div class="header-title">
<h2><?php echo (isset($pageTitle)) ? $pageTitle : 'Test'?></h2>
</div>
</div>
This code snippet runs perfectly and on every page.
I am trying to set the $pageTitle dynamically. This means that I am trying to set it for every page differently.
I have set inside the UsersController.php
$this->set('pageTitle', 'Nebojsa');
In index method.
But when I go to the url /users/index , this page title stays 'test'.
I have also tried to assign the value inside the Template/Users/index.ctp
<?= $this->assign('pageTitle', __('Users')); ?>
But it won't work.
What am I doing wrong ? Could you maybe point me in the right direction, where should I look ?
Between view templates and layouts you can use:
<?= $this->assign('pageTitle', __('Users')); ?>
And get the value back in any other template or layout using:
<?= $this->fetch('pageTitle') ?>
As far as setting the title from the controller your code is correct.
$this->set('pageTitle', 'Nebojsa');
<?php echo (isset($pageTitle)) ? $pageTitle : 'Test'?>
Related
i have figured out some blade templates (laravel) like #section('title', getOption('app_name') . ' - Login')
#section('body') as its real php formis <?php $__env->startSection('title', getOption('app_name') . ' - login'); ?>
<?php $__env->startSection('content'); ?> . and these are correct ,,
But i wanna know the real php form of #extends(layouts.app) .
anybody got an Idea ?
All of your compiled blade views are stored in storage/framework/views/ if you want to dig around and try to understand how Laravel turns a blade template into php.
It looks like your views define the sections first, then pass them to the layout
as variables. Inside your view which #extends(layouts.app) you might see this at the end:
<?php echo $__env->make('layouts.app', \Illuminate\Support\Arr::except(get_defined_vars(), array('__data', '__path')))->render(); ?>
The sections were defined before that line and passed as variables. When you look inside the layout itself, you'll see things like this for including each of those sections:
<?php echo $__env->yieldContent('content'); ?>
Basically in default.ctp I have this for my title:
<title>
<?= $this->fetch('title') ?>
</title>
And inside of the controller I have this line:
$this->set('title', 'Test-Title');
But it does nothing, it still displays controllers name(Jobs, controllers full name os JobsController.ctp)
But if I put this inside of my view file:
$this->assign('title', 'Test-Title');
It changes the title. So what is wrong with $this->set('title', $title) ?
fetch() returns the contents of a block not a variable. Using set() in your Controller is setting a variable that can be output in your View templates by echoing the variable:-
<?php echo $title; ?>
If you want to use fetch() you need to use it in combination with assign() in the View templates to define the block. For example in your View template use:-
<?php $this->assign('title', $title); ?>
And then in the layout template:-
<title><?php echo $this->fetch('title'); ?></title>
In CakePHP 3 the idea is to set the page title by assigning it in the View as it relates to the rendering of the page. This differs from how this was originally handled in CakePHP 2 where you'd define title_for_layout in your controller and then echo the $title_for_layout variable in the layout template (this was deprecated in favour of the CakePHP 3 approach in later versions of Cake 2).
You can just set() the variable in your controller:
// View or Controller
$this->set('title', 'Test-title');
Then use it as a standard variable is in your layout or view:
<!-- Layout or View -->
<title>
<?php echo $title; ?>
</title>
Details here: http://book.cakephp.org/3.0/en/views.html#setting-view-variables
Using assign() is different, which is why it works with fetch(). assign() is used with View Blocks: http://book.cakephp.org/3.0/en/views.html#using-view-blocks
In CakePHP 3 layout template, make sure to set the title as below.
<title>
<?= $this->fetch('title') ?>
</title>
Then in your view:
<?php $this->assign('title', 'Title Name'); ?>
This is the way CakePHP will use its built-in View classes for handling page title (view blocks) rendering scenarios.
Just for completion, I came across a situation where a malformed .js script with undefined variables referenced between <head></head> resulted in the <title></title> tags being posted to DOM (showed in page source) but Chrome, Firefox and (from memory) MSIE all failed to deliver the content of the title to the APP UI, again from memory - iOS mobile was unaffected.
If you want stick to your code, after setting "title" variable just simply write this:
<?= __('Main Project Name') ?>
<?php if( isset($title)) $this->assign('title', $title); ?>
<?= ' - ' . $this->fetch('title') ?>
I have done this, this way in default.ctp
<?php
$cakeDescription = __d('cake_dev', 'Your Title');
?>
<title>
<?php echo $cakeDescription ?>: <?php echo $title_for_layout; ?>
</title>
In my view file, I have done this.
<?php $this->assign('title', 'Your Title');?>
I have such type of code in view, add.ctp file in Cake PHP.
<div id="container">
<div id="content">
------------------
</div>
<div id="sidebar">
----------------
</div>
</div>
Now in Layout, in default.ctp file, we access this code by this line.
<?php echo $this->fetch('content'); ?>
I have sidebar in each and every view file, and if I need some changes then I will go in each and every file and then change.
Now My Question is that, can I made a file in layout like sidebar.ctp or any thing else that I just call this file in my view. If I can, then how I will made such type of file.
You could do it with include or elements like this
<?php echo $this->element('sidebar'); ?>
With the element, you make the sidebar.ctp file in the View/Elements/ folder.
Check for more information: Cakephp 2 Elements
The other way is with include (not my choice, but another way to accomplish it)
<?php include('../View/Layouts/sidebar.ctp'); ?>
You can use elements and if the content in elements is dynamic you can use the blocks supported in latest version of cakephp.
http://book.cakephp.org/2.0/en/views.html
I'm struggling trying to understand cakephp's views, blocks and layouts.
I need everypage to show a left and right sidebar which content might change. At this moment I have the right sidebar defined in /pages/home.ctp but I'm guessing it would be better to extend that sidebar since it has to appear in everypage. Correct me if that thought is wrong.
Then, I have this view add.ctp for the 'usuarios' table, it practically shows the fields login and password. I want to show this view in the sidebar, but I'm really lost as how to do that.
Thanks in advance.
Lets make this thing easy. Like #patrick said, there is a lots of way.
Start with layout file. Rearrange your default.ctp layout like-
default.ctp layout
<div id="container">
<div id="header">
<?php echo $this->element('header');?>
</div>
<div id="left-sidebar">
<?php echo $this->element('left-sidebar');?>
</div>
<div id="content">
<?php echo $this->Session->flash(); ?>
<?php echo $this->fetch('content'); ?>
</div>
<div id="right-sidebar">
<?php echo $this->element('right-sidebar');?>
</div>
<div id="footer">
<?php echo $this->element('footer');?>
</div>
</div>
Now create elements ctp files as header.ctp, left-sidebar.ctp, right-sidebar.ctp and so on and place them to app/View/Elements.
Your left-sidebar.ctp file may looks like this...
left-sidebar.ctp
// to show login form //
if you just need to show on view.ctp place few logic here for login form.
//end login form//
show other sidebar contents
There are a couple ways to do it, depending on your Cake version. If you're using >=2.1 (which I assume you are since you asked about blocks), then you should try those to see if they work for your setup. The way I usually do things is that if all views for a controller need common markup then those view files would extend a base view within the Controller directory, e.g.
#/View/Posts/index.ctp
<?php
$this->extend('_skel'); //arbitrary filename, I use '_skel' since that makes sense
echo $this->Html->para(null, 'Hello');
#/View/Posts/_skel.ctp
<?php
echo $this->Html->div('sidebar', 'Sidebar for posts...');
echo $this->fetch('content'); // This gets all output from the Posts/index.ctp view
Then all your Posts views which extend _skel will have the sidebar automatically.
Your login module might make sense as an element - something that could be used anywhere in your views.
This is my very first question at StackOverflow.
I was trying to post comment in the post Can a Joomla module "know" what position it's in?, but could find no way to do that so I have to post another question here. Any tips regarding how to do this properly is greatly apprieciated.
Anyway, here is my questions:
I tried the code mentioned in the above post but it seemed didn't work. I'm not only quite new to PHP, but also coding, so I'm not sure if it's me or the code. here's what I did to the module's default.php file:
1)to ensure I insert the code at the right place, I insert
<?php echo(print_r($module)); ?>
and it output 1 at the right position;
2)the position-name that I need to determine is "showcase-a", so I insert code
<?php if ($module->position == 'showcase-a'):?>test<?php endif;?>
to the above place, but this time it does't show anything;
3)then I tried this code:
<?php if ($module):?><span>test</span><?php endif; ?>
But still it does't display "test" at the position as I expected.
4)I tried
<?php if (1):?><span>test</span><?php endif; ?>
and the test "test" displays as good. So I didn't code the IF statement wrong.
Now I'm totally lost. Since print_r($module) outputs 1, $module must be positive, why PHP ignore test in 3)? This is just a side question for the sake of PHP learning. What I still need to solve is let the module determine which position itself is in.
Please help. Thank you!
I'm not sure about using it in the template (although it doesn't seem wrong altogether) but my modules oftentimes access the position like this:
$module->position
in the module (so mod_something.php) so try to put it there, if it's available just set a variable and it will be available in the view too.
If $module->position didn't work for you. $module->position may only work for joomla 1.7, 2.5 and + but i'm not sure. $module->position works in joomla 2.5.
Otherwise you need to make a module.php function
Check out the file
/templates/you-template/html/modules.php
For example you make your position the following in your template (for example index.php)
<jdoc:include type="modules" name="header" style="includeposition" />
inside of
/templates/you-template/html/modules.php
You make a function like this :
<?php
function modChrome_includeposition($module, &$params, &$attribs){
//Set a value to $module->position
$module->position='WHATEVER YOUWANT';
?>
<div class="moduletable <?php echo $params->get('moduleclass_sfx').' '.$module->name; ?>">
<?php
if($module->showtitle){
?>
<div class="modtitle">
<div class="fleche">
<span>
<?php
echo $module->title;
?>
</span>
</div>
</div>
<?php
}
?>
<div class="modcontent">
<?php echo $module->content; ?>
<div class="clear">
</div>
</div>
</div>
<?php
}
?>
make different functions names that sets different values if needed.
the function name will match the syle you set:
modChrome_includeposition($module, &$params, &$attribs)
because
style="includeposition"
inside of your jdoc.