View inside a view using CakePHP 3 - cakephp

Is it possible to display a view inside another view?
I have the following code:
<?php if ($result->type === 'brochure') : ?>
<div>
// massive template block
</div>
<?php elseif ($result->type === 'library') : ?>
<div>
// massive template block different from above
</div>
<?php else : ?>
<div>
// massive template block different from both above
</div>
<?php endif; ?>
I want to replace those with a content block so to speak. I had a look at view blocks but I'm either using it wrong or it doesn't do what I want it to do.
Is this possible in CakePHP 3?

you can use elements for that.
first you should create elements in src/Template/Element directory with .ctp format like this
// in brochure.ctp file in src/Template/Element
<div>
// your massive template block
</div>
then you can call elements like this :
<?php if ($result->type === 'brochure') : ?>
<?= $this->element("brochure") ?>
<?php elseif ($result->type === 'library') : ?>
<?= $this->element("library") ?>
<?php else : ?>
<?= $this->element("default") ?>
<?php endif; ?>

Related

How to show only 2 post from post in ACF relationship

After creating customfield from ACF. I select multiple articles. How can I show only the last 2 posts? Thank!
Show only 2 post from relationship ACF
I would use the post_object field. With this field, you can select which objects you would like to display, when you configure it to "multiple values".
<?php
$featured_posts = get_field('featured_posts');
if( $featured_posts ) { ?>
<ul>
<?php foreach( $featured_posts as $post ) {
setup_postdata($post); ?>
<li>
<?php the_title(); ?>
</li>
<?php } ?>
</ul>
<?php
wp_reset_postdata(); ?>
<?php } ?>

Use PaginatorHelper in Cells CakePhp 3

Hi I'm trying to use Paginator within a Cell the Paginator works rigth but the PaginatorHelper in the Template of the cell just doesn't work.
I'm using CakePhp 3.5
Here is my code
src/View/Cell/FinderCell.php
namespace App\View\Cell;
use Cake\View\Cell;
use Cake\Datasource\Paginator;
class FinderCell extends Cell {
public function display() {
$this->loadModel('Pokemon');
$paginador = new Paginator();
$pokemons = $paginador->paginate(
$this->Pokemon->find()
);
$this->set(compact('pokemons'));
}
}
src/Template/Cell/Finder/display.ctp
<?php
foreach ($pokemons as $pokemon) :
?>
<div class="tipo form large-2 medium-2 columns content">
<?php echo $this->Html->image($pokemon->pokemon_image) ?>
</div>
<?php endforeach; ?>
<div class="paginator">
<ul class="pagination">
<?= $this->Paginator->prev('<< Anterior') ?>
<?= $this->Paginator->numbers() ?>
<?= $this->Paginator->next('Siguiente >>') ?>
</ul>
<p><?= $this->Paginator->counter() ?></p>
</div>
I get this
If you want to use the helper, then you need to populate either the request object with the pagination parameters, which is what the paginator component would normally do for you:
// ...
$this->request = $this->request->withParam(
'paging',
$paginator->getPagingParams() + (array)$this->request->getParam('paging')
);
$this->set(compact('pokemons'));
or the paginator helper, which in turn sets the parameters on the request object:
// ...
$pagingParams = $paginator->getPagingParams();
$this->set(compact('pokemons', 'pagingParams'));
$this->Paginator->options(['paging' => $pagingParams]);
See also
API > \Cake\Datasource\PaginatorInterface::getPagingParams()
API > \Cake\View\Helper\PaginatorHelper::options()

Cakephp working whit Cell

Hi I have a trouble whit Cell in my application I follow the tutorial but did not work for me
here is my code https://github.com/boguda/okpionir.git
I bake cell for News and add display function
I want to use this cell on my static page src/Template/Pages/pocetna.ctp
but it doesn't work
just change
<?php $cell = $this->cell('News'); ?>
<?= $cell ?>
to
<?= $this->cell('News::display'); ?>
Call Cell has to refer to specific method name
EDIT:
Also you have an error in your src/View/Cell/NewsCell
public function display()
{
$this->loadModel('News');
----> $top_news= $this->News->find()
->select('title')
->order(['created'=>'DESC'])
->limit(3)
->toArray();
$this->set('top_news' => $news); <-----
}
should be:
$this->set('top_news', $top_news);
or
$this->set(compact('top_news'));
And your view Template/Cell/News/display.ctp might look like this
<ul class="list-group">
<?php foreach ($top_news as $news): ?>
<li><?= h($news['title']); ?></li>
<?php endforeach; ?>
<li class="list-group-item">Second item</li>
<li class="list-group-item">Third item</li>
</ul>

I can't load blocks in my default.ctp

I want to use the blocks statements in my application. I have a theme called: Boostrap. If I use only the statement $this->element() my elements load without problems, but if I use $this->start() and $this->end() I can't load my elements.
I try create a new file called index.ctp for a TestsController but the result is the same. I put $this->fetch('header') and $this->fetch('footer') in this file and nothing.
That's only an example of my real structure:
Themed\Boostrap\Layouts\default.ctp:
<html>
<head>
<title></title>
</head>
<body>
<?php $this->start('header'); ?>
<?php echo $this->element('header'); ?>
<?php $this->end(); ?>
<?php echo $this->Session->flash(); ?>
<?php echo $this->fetch('content'); ?>
<?php $this->start('footer'); ?>
<?php echo $this->element('footer'); ?>
<?php $this->end(); ?>
</body>
</html>
Themed\Boostrap\Elements\header.ctp:
<header>
<h1>Hello World!!</h1>
</header>
Themed\Boostrap\Elements\footer.ctp:
<!-- app/views/themed/boostrap/elements/footer.ctp -->
<footer>
Copyright (c) Bla bla bla 2014.
</footer>
Details of my environment:
S.O: Windows 7
CakePHP Versión: 2.4.4
PHP Versión: 5.3.28
You're layout should define the block output, not the start and end methods:
<?php echo $this->fetch('header'); ?>
Then, in your view, you outline the block that will be outputted:
<?php $this->start('header'); ?>
<?php echo $this->element('header'); ?>
<?php $this->end(); ?>
Given this use case, blocks don't seem like the right solution. Your element seems like it would be the only thing to display, in which you would output the element on the layout. However, a more appropriate use case would be different output at a per-page level.
In your layout:
<?php echo $this->element('header'); ?>
<?php echo $this->fetch('header_block'); ?>
Page one:
<?php $this->start('header_block'); ?>
//Something unique to page 1
<?php $this->end(); ?>
Page two:
<?php $this->start('header_block'); ?>
//Something unique to page 2
<?php $this->end(); ?>

Is it possible to get a different header.php on language switch with qtranslate (Wordpress)?

the problem is that on home page I have a lot of code which is added through header.php and it is not amendable through admin panel. What I want to do is to switch header files on language switch. Whenever a user press on the language icon, lets say on the English language icon the header should also switch from <?php get_header('mylanguage') ?> to <?php get_header('english') ?> or etc. Is it possible to do like this?
Regards,
In your page template instead of calling header through:
get_header();
use this to enable header switching when changing languages;
<?php if (qtrans_getLanguage() == 'de'): ?>
<?php include(TEMPLATEPATH.'/header-home-de.php' ); ?>
<?php else : ?>
<?php include(TEMPLATEPATH.'/header-home-en.php' ); ?>
<?php endif; ?>
In this example there are 2 headers depending on selected language.
Surely, it is possible in qTranslate. Even I've done qTranslate Swicher in bootstrap navigation.
<?php } if (qtranxf_getLanguage() == 'en') { ?>
English
<ul class="dropdown-menu">
<?php echo qtranxf_generateLanguageSelectCode('code'); ?>
</ul>
<?php } elseif (qtranxf_getLanguage() == 'fr') { ?>
Français
<ul class="dropdown-menu">
<?php echo qtranxf_generateLanguageSelectCode('code'); ?>
</ul>
<?php } ?>

Resources