Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix. Parser allow other extension #2264

Merged
merged 3 commits into from
Sep 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions system/View/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,13 @@ public function render(string $view, array $options = null, bool $saveData = nul
$saveData = $this->config->saveData;
}

$view = str_replace('.php', '', $view);
$fileExt = pathinfo($view, PATHINFO_EXTENSION);
$view = empty($fileExt) ? $view . '.php' : $view; // allow Views as .html, .tpl, etc (from CI3)

// Was it cached?
if (isset($options['cache']))
{
$cacheName = $options['cache_name'] ?: $view;
$cacheName = $options['cache_name'] ?? str_replace('.php', '', $view);

if ($output = cache($cacheName))
{
Expand All @@ -142,7 +143,6 @@ public function render(string $view, array $options = null, bool $saveData = nul
}
}

$view = $view . '.php';
$file = $this->viewPath . $view;

if (! is_file($file))
Expand Down
8 changes: 8 additions & 0 deletions tests/system/View/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -941,4 +941,12 @@ public function testRenderStringSavingData()
$this->assertArrayNotHasKey('testString', $parser->getData());
}

public function testRenderFindsOtherView()
{
$parser = new Parser($this->config, $this->viewsDir, $this->loader);
$parser->setData(['testString' => 'Hello World']);
$expected = '<h1>Hello World</h1>';
$this->assertEquals($expected, $parser->render('Simpler.html'));
}

}
1 change: 1 addition & 0 deletions tests/system/View/Views/Simpler.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h1>{testString}</h1>
6 changes: 4 additions & 2 deletions user_guide_src/source/outgoing/view_parser.rst
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ What It Does
============

The ``Parser`` class processes "PHP/HTML scripts" stored in the application's view path.
These scripts have a ``.php`` extension, but can not contain any PHP.
These scripts can not contain any PHP.

Each view parameter (which we refer to as a pseudo-variable) triggers a substitution,
based on the type of value you provided for it. Pseudo-variables are not
Expand Down Expand Up @@ -99,7 +99,9 @@ View parameters are passed to ``setData()`` as an associative
array of data to be replaced in the template. In the above example, the
template would contain two variables: {blog_title} and {blog_heading}
The first parameter to ``render()`` contains the name of the :doc:`view
file </outgoing/views>` (in this example the file would be called blog_template.php),
file </outgoing/views>`, Where *blog_template* is the name of your view file.

.. important:: If the file extension is omitted, then the views are expected to end with the .php extension.

Parser Configuration Options
============================
Expand Down