6

How can I change the extension for CakePHP Views from .ctp to .php

I have seen there is this line in /cake/libs/view.php var $ext = '.ctp'; that sets the extension but how can I do it from my /app/ folder so it doesn't effect Cake core files.

Thanks

4 Answers 4

10

You can set the extension in your AppController with

public $ext = '.yourext';
2
  • What about multiple extensions like accept both .php and .ctp is that possible?
    – Cameron
    Jan 8, 2011 at 21:38
  • You cannot specify multiple extensions, however it seems like there is a fall back to .ctp if no .php file is found. This doesn't work for elements, though (I opened a ticket for this issue).
    – dhofstet
    Jan 9, 2011 at 9:53
1

This is is reply to Cameron's comment regarding the issue of using multiple extensions in light of the fact cakephp does not allow you to specify multiple extensions.

I am using Mustache for a single site that uses merb, rails2, rails3 and cakephp for different sections of the site. The cake site "receives" mustache files for common layout elements but these templates have a '.mustache' file extension which my cake site will not recognize. My workaround is basically what dhofstet suggests just framed in the context of your specific usecase. In short, create a wrapper that might look something like this:

<?
  $tmp = $this->ext;
  $this->ext = '.mustache';
?>
<?= $m->render($this->renderElement('moznav/advanced_header'), array('foo' => $bar)) ?><br />

<? $this->ext = $tmp; ?>

When flow returns to the caller, you keep on using your native file extension.

0

How can I change the extension for CakePHP Views from .ctp to .php

I have seen there is this line in /cake/libs/view.php var $ext = '.ctp'; that sets the extension but how can I do it from my /app/ folder so it doesn't effect Cake core files.

example:

  • you have view posts/add.ctp
  • now you rename add.ctp into add.php
  • and then you run .../posts/add the message error show:

    Error: The view for PostsController::add() was not found.

    to your app can understand extention .php, you add line public $ext = '.php' in PostsController.php

  • now, you run again ..posts/add => okie, cakephp understand extention .php

Notice: if you use atrribute $ext = '.php' but file view named .ctp, cakephp extention .ctp will use by default

0

I found this post because I had the same problem. This is not mentioned in the Predominant TwigView plugin documentation on Github. I'm tired of those documentations that explains only half of things and with which we have to guess the second half. This is a big waste of time that slows down projects pointlessly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.