View previous topic :: View next topic |
Author |
Message |
shoulders Smarty Rookie
Joined: 05 May 2010 Posts: 11
|
Posted: Mon May 07, 2018 11:16 am Post subject: Install smarty via php composer |
|
|
Hi
I have written my own CRM QWcrm at https://github.com/shoulders/qwcrm and it all works fine using smarty as its template engine.
I have loaded MoTranslator via composer and got it to work following their instructions but I cannot figure out how to get smarty to be autoloaded via composer.
1) I have installed smarty via composer using composer require smarty/smarty and this has brought the files down into my vendor folder.
2) according to my research packages that have autoload functionality are autoloaded and some people say you have to declare smarty to auto load.
Question
So i have all of the files in place in the vendor folder, how to I enable Smarty to be started and used in my code?
If there are 2 ways of doing this (i.e. manuall / using composer auotload) I would appreciate both version so I can add it to my notes and the smarty documentation unless I have missed it.
Thanks
shoulders |
|
Back to top |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Mon May 07, 2018 12:14 pm Post subject: Re: Install smarty via php composer |
|
|
shoulders wrote: | 2) according to my research packages that have autoload functionality are autoloaded and some people say you have to declare smarty to auto load. |
If you have installed Smarty through Composer, it is already embedded into Composer's autoload system and you don't need to do anything special to use it.
Quote: | So i have all of the files in place in the vendor folder, how to I enable Smarty to be started and used in my code? |
It is already enabled. At most, invoke
Code: | composer dump-autoload |
if you've chnaged anything that may stop autoloading from behaving.
Quote: | If there are 2 ways of doing this (i.e. manuall / using composer auotload) I would appreciate both version so I can add it to my notes and the smarty documentation unless I have missed it. |
Manual way only applies if you've installed Smarty manually. |
|
Back to top |
|
shoulders Smarty Rookie
Joined: 05 May 2010 Posts: 11
|
Posted: Mon May 07, 2018 12:19 pm Post subject: |
|
|
should the following code still work?
Code: |
$smarty = new smarty;
$smarty->template_dir = THEME_TEMPLATE_DIR;
$smarty->cache_dir = SMARTY_CACHE_DIR;
$smarty->compile_dir = SMARTY_COMPILE_DIR;
$smarty->force_compile = $QConfig->smarty_force_compile;
|
This code does not work when using the autoload method. Als namespaces dont seem to of been created, is this normal?
And just to confirm I do not have to do the following?
Code: |
// Set Path for SMARTY in the php include path
set_include_path(get_include_path() . PATH_SEPARATOR . LIBRARIES_DIR.'smarty/');
require_once('Smarty.class.php')
|
|
|
Back to top |
|
shoulders Smarty Rookie
Joined: 05 May 2010 Posts: 11
|
Posted: Mon May 07, 2018 3:30 pm Post subject: |
|
|
to answer my questions above
Code: |
$smarty = new smarty;
$smarty->template_dir = THEME_TEMPLATE_DIR;
$smarty->cache_dir = SMARTY_CACHE_DIR;
$smarty->compile_dir = SMARTY_COMPILE_DIR;
$smarty->force_compile = $QConfig->smarty_force_compile;
|
you can use the code above (altered for your use) but there is one difference that you need to make sure you change otherwise it will not work on the newest version of smarty. please note I was running an older version of smarty that used:
Code: | $smarty = new smarty; |
the new class needs to have a capital giving:
Code: | $smarty = new Smarty; |
The require/include group of code is not needed when using autoload.
perhaps add an example of this in COMPOSER_RELEASE_NOTES.txt
thanks |
|
Back to top |
|
scuzzy Smarty Regular
Joined: 31 Aug 2003 Posts: 84
|
Posted: Mon May 07, 2018 10:07 pm Post subject: |
|
|
My composer file looks like this
Code: | {
"require": {
"smarty/smarty": "^3.1"
}
} |
My PHP looks like this
Code: | require( 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php' );
$smarty = new Smarty(); |
And it just works |
|
Back to top |
|
shoulders Smarty Rookie
Joined: 05 May 2010 Posts: 11
|
Posted: Tue May 08, 2018 7:55 am Post subject: |
|
|
@AnrDaemon and @scuzzy, thanks for the help
@scuzzy
try commenting out
Code: | require( 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php' ); |
You might not need it if smarty was installed with composer. |
|
Back to top |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Tue May 08, 2018 8:55 am Post subject: |
|
|
shoulders wrote: | @AnrDaemon and @scuzzy, thanks for the help
@scuzzy
try commenting out
Code: | require( 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php' ); |
You might not need it if smarty was installed with composer. |
You need it, but only in your core include file.
Also, it's rather overload to use DIRECTORY_SEPARATOR to include files.
Just use '/'. You only need DIRECTORY_SEPARATOR, when constructing paths for user to see or for parsing the return from the OS, and even then, weight carefully if it ain't easier to flat out convert '\' to '/' and go ahead? |
|
Back to top |
|
shoulders Smarty Rookie
Joined: 05 May 2010 Posts: 11
|
Posted: Tue May 08, 2018 9:00 am Post subject: |
|
|
oops!
I see what I did wrong. the autoload.php is for the whole composer autoload process not just smarty.
thanks for the heads up |
|
Back to top |
|
|