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

Including a file with relative path changes the cwd of the including file #246

Closed
Stadly opened this issue Jun 14, 2016 · 6 comments
Closed

Comments

@Stadly
Copy link

Stadly commented Jun 14, 2016

In some cases with complex inheritance, including one file ruins for other file inclusions, resulting in unsuccessful template compilation. Consider the following example:

{* /index.php *}
<?php
require_once 'Smarty/Autoloader.php';
$Smarty = new Smarty();
$Smarty->setTemplateDir(__DIR__.'/Tpl');
$Tpl = $Smarty->createTemplate(__DIR__.'/Edit.tpl');
$Tpl->display();


{* /Edit.tpl *}
<form method="post" enctype="multipart/form-data">
   <fieldset>
{include 'Collection/Edit.tpl'}
   </fieldset>
</form>


{* /Tpl/Collection/Edit.tpl *}
{extends '../Container.tpl'}
{block Buttons}
   {include '../General.tpl' Button=[Link => '/admin', Title => _('Cancel')]}
   {include '../Primary.tpl' Button=[Name => 'edit', Type => 'submit', Title => _('Save')]}
{/block}


{* /Tpl/Container.tpl *}
<div class="buttons">
{block Buttons}{/block}
</div>


{* /Tpl/General.tpl *}
{extends './Abstract.tpl'}
{block Class}_button transparent{/block}


{* /Tpl/Primary.tpl *}
{extends './Abstract.tpl'}
{block Class}_button{/block}
{block Icon}<i class="_icon-internal-white"></i>{/block}


{* /Tpl/Abstract.tpl *}
{if isset($Button.Link)}
<a class="{block Class}{/block}" role="button" href="{$Button.Link}"><span>{$Button.Title}</span>{block Icon}{/block}</a>
{else}
<button type="{$Button.Type}" class="{block Class}{/block}" name="{$Button.Name}"><span>{$Button.Title}</span>{block Icon}{/block}</button>
{/if}

This will result in a Fatal error: Uncaught --> Smarty: Unable to load template file '../Primary.tpl' in 'file:../Container.tpl'.

If you change the order of the buttons in /Tpl/Collection/Edit.tpl:

{* /Tpl/Collection/Edit.tpl *}
{extends '../Container.tpl'}
{block Buttons}
   {include '../Primary.tpl' Button=[Name => 'edit', Type => 'submit', Title => _('Save')]}
   {include '../General.tpl' Button=[Link => '/admin', Title => _('Cancel')]}
{/block}

You get Fatal error: Uncaught --> Smarty: Unable to load template file '../General.tpl' in 'file:../Container.tpl'.

So it seems that the first include is always successful, while the second include fails. In other words, both includes are fine, but the first one ruins for the second one. Indeed, if you remove one of the buttons, (either the line {include '../Primary.tpl' Button=[Name => 'edit', Type => 'submit', Title => _('Save')]}or the line {include '../General.tpl' Button=[Link => '/admin', Title => _('Cancel')]}, the template compiles successfully.

I suspect that this has to do with the relative paths. After including one of the files, the working directory is changed, so that the second file is not found. A simple test more or less confirms this, as the template compiles successfully when the second include's path is changed from ../Primary.tpl to ./Primary.tpl.

I am using the latest development build 3.1.30-dev of Smarty.

@Stadly
Copy link
Author

Stadly commented Jul 13, 2016

Have no one else experienced this?

It would be really nice if someone could try my code and confirm whether they get the same error or not.

If anything is unclear in my bug description, please let me know so I can clarify.

@uwetews
Copy link
Contributor

uwetews commented Jul 14, 2016

I'm working an a fix.

@Stadly
Copy link
Author

Stadly commented Jul 14, 2016

Great! Thanks! :)

@uwetews
Copy link
Contributor

uwetews commented Jul 18, 2016

The fix is now in the master branch

@uwetews uwetews closed this as completed Jul 18, 2016
uwetews added a commit that referenced this issue Jul 18, 2016
uwetews added a commit that referenced this issue Jul 18, 2016
…ock} could fail #246 (reverted from commit 09d8eab) (reverted from commit 3fdd9e4)
uwetews added a commit that referenced this issue Jul 18, 2016
…ock} could fail #246 (reverted from commit 09d8eab) (reverted from commit 3fdd9e4)
@uwetews uwetews reopened this Jul 18, 2016
@uwetews
Copy link
Contributor

uwetews commented Jul 18, 2016

Sorry, not completely solved

@uwetews
Copy link
Contributor

uwetews commented Jul 19, 2016

Now it's done

@uwetews uwetews closed this as completed Jul 19, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants