mto
mto

Reputation: 11

variable in the Layout

I am using CakePHP on my website and I need to parse a variable and write it in the layout. The variable is in the MySQL, table "articles", column "author".

If inside an article I write it shows me the author, but if I write it in the views/layout/default.thtml it doesn't show me anything.

Is there a simple way to do it?

Upvotes: 0

Views: 107

Answers (2)

AKKAweb
AKKAweb

Reputation: 3817

One option to do this is by using CakePHP's requestAction and place it in an element you can tag to the default layout. Basically you would do something similar to the following

//Create a function in your articles controller as such
function authorName($articleId = NULL){    
   //Return Author if an ID is passed
    if(!empty($articleId)){
        $this->Article->recursive = -1;
        $article = $this->Article->findById($articleID);

        $return $article['Article']['author'];
    }

}

// Then create an element author_name.ctp and place it in views/elements 
// that requests the AuthorID form the above function
<div>
<?php 
    $author = $this->requestAction(
        array(
            'controller' => 'articles', 
            'action' => 'authorName'
        ), 
        array(
            'pass' => $article_id
        )
    );
    echo $author;
?>
</div>

// Then in you default layout or anywhere else 
// you can include the element as such
// You have to figure out a way to get the $id. If you can place a screenshot
// Or some code of you default layout area where you need this, we can help you further
<?php echo $this->element('author_name', array('article_id' => $id)); ?>

Upvotes: 0

Yises
Yises

Reputation: 329

Are you obtaining this variable in the right controller function? Maybe you need to insert it in a beforeRender function (http://planetcakephp.org/aggregator/items/1398-utilizing-the-appcontrollerbeforerender-to-assign-cakephps-controller-attribut)

Upvotes: 1

Related Questions