homepage Welcome to WebmasterWorld Guest from 54.204.215.209
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Subscribe to WebmasterWorld
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
PHP Language Files and Templates
Sp4rkyM4rk




msg:4184494
 10:47 pm on Aug 9, 2010 (gmt 0)

I'd like to create a system which uses language files written in PHP, where the variables inside can be declared in HTML files, but the end result is the HTML files will be included and processed by a PHP file.

I understand there is a way to do this easily with variables; however, I'd rather seperate the project and 'save' using the typical variable format ($varname), so I can immediately tell which parts of code relate to the template and which parts relate to the PHP code itself, sort of like phpBB3. I'd also like to use this in conjunction with the trigger_error() function, so I can have all error messages in one array rather than declaring them in different variables.

I have learnt a bit from looking at the phpBB files, and was able to produce the short code below.

<?php

$template -> assign_vars(array(
'NOT_AUTHORISED' => 'You are not authorised to access this area.',
'NOT_ACTIVE' => 'You need to activate your account before you can use this feature, please check your e-mail.',
'LOGGED_IN_AS' => 'You are logged in as '
));

?>


These are called in the HTML file as something like "{LOGGED_IN_AS}", to return the matching variable result: 'You are logged in as '. However, whenever I have tried this I'm given the following error:

Fatal error: Call to a member function assign_vars() on a non-object in /PATHS/ on line 3


Please could someone point me in the right direction to make this work?

Thanks in advance. :)

 

Matthew1980




msg:4184642
 7:13 am on Aug 10, 2010 (gmt 0)

Hi there Sp4rkyM4rk,

Unless I miss the point, surely you can define these as constants as they are never changing value, then all you need to do is include the file where ever you want, (in the header file for example) then these values are available to all of your project, for this sort of thing there is no need to set an array for this use, when constants are built for the job ;)

Typically:-
<?php
//Definitions file :)
define('NOT_AUTHORISED','You are not authorised to access this area.');
define('NOT_ACTIVE' ',''You need to activate your account before you can use this feature, please check your e-mail');
define('LOGGED_IN_AS','You are logged in as:');
?>

Then so long as you include the file you just reference them like so:-

echo LOGGED_IN_AS." ".$_POST['name'];

You see the logic there..

Hope that makes sense.

Cheers,
MRb

Sp4rkyM4rk




msg:4184824
 2:35 pm on Aug 10, 2010 (gmt 0)

Thanks a lot, this works fine when calling the constants within PHP for error messages. Although I'd also like to use them within the template files which will be included on the PHP page in the end. I'd like the message variables to be the same, but the layout of the page to be different depending on the theme the user has chosen.

I guess I could use an if statement within the PHP page itself to do this, such as:

if ($theme_id == 1 && $lang_id == 1)

{

require ('en_definitions_t1');

}

else

{

require ('en_definitions_t2');

}


(T# representing another theme ID)

Thanks again. :)

Sp4rkyM4rk




msg:4186617
 4:05 pm on Aug 13, 2010 (gmt 0)

Sorry to bump this, but I've come across another issue. It seems these constants cannot be called from a MySQL Database, so 'MEMBER' in the database will output 'MEMBER' on the page. My definition in the definitions file is "Registered member". My query picks up information from two tables, as follows:

$groupquery = mysql_query('SELECT users.usergroup_id, usergroups.group_id, usergroups.group_rank_name FROM users, usergroups WHERE users.usergroup_id = usergroups.group_id AND users.username = "' . $usersession . '"');
$rows = mysql_fetch_assoc($groupquery)

$group_name = $rows['group_name'];

echo 'Your group: ' . $group_name . ';


So whenever I add a new usergroup, the user can be updated by changing only their Group ID. However, when it comes to multilingual support, picking up the direct value minus the definition is an issue. Is there any way to get around this?

Thanks again. :)

enigma1




msg:4187022
 1:50 pm on Aug 14, 2010 (gmt 0)

What do you mean constants cannot be called from the database? If you want to keep constants/definitions in the database you do the queries and then the defines.

$group_name = $rows['group_name'];
define($group_name, $string);

where $string is the value for the definition which you could also keep in the dbase.

For the languages/templates, it's going to be driven by the structure of your database or files whatever you use for storage. For instance, using a method of different columns in the same database table to separate language strings or template strings will force you to create all kinds of loops and conditionals in the scripts.

The typical process is to retrieve the user input/choice, validate it and invoke/define/process the template and language. For example:

$lang = isset($_GET['lang'])?(int)$_GET['lang']:1;
if( !isset($lang_array[$lang]) ) $lang = 1;
require('languages/' . $lang);

keep it simple and eliminate conditionals where possible.

Sp4rkyM4rk




msg:4187495
 3:43 am on Aug 16, 2010 (gmt 0)

I don't want to store them in the database. I mean the constant couldn't be "converted" into its string when it is called from the database. Example:

<?php

define('EXAMPLE_GROUP','This is an example group.');

$query = mysql_query('SELECT usergroup FROM users WHERE username = "' . $_SESSION['username'] . "');
$rows = mysql_fetch_assoc($query);

$usergroup = $rows['usergroup'];

echo 'Your rank is' . $usergroup; // Output: 'EXAMPLE_GROUP'

echo 'Your rank is' . EXAMPLE_GROUP; // Output: This is an example group.

?>


The constant name is stored in the database but doesn't seem to recognise the constant which was already defined before we called for the data.

Thanks for the tip on the language files. To be honest I'm stumped about templates. I guess I could do that using if statements based on what style the user has selected, and say which files should be required effectively changing the layout.

Another thing related to this is use of PHP-parsed if statements in HTML, as in PHP, too many make the script more confusing to follow. Much tidier to say "<!-- IF ADMIN_ACCESS -->Visit the ACP<!-- ENDIF -->", but I have no idea. :(

EDIT: I've done some searching, and found the type of thing I'm after is called a 'placeholder', where the data in curly braces in HTML template files is parsed by PHP.

Matthew1980




msg:4187538
 7:22 am on Aug 16, 2010 (gmt 0)

Hi there Sp4rkyM4rk,

Enigma was correct in what he was suggesting, this is how you can dynamically alter constants, when done in this fashion you can think of them as switches, because you can use them to be triggered like a switch depending on the state of the stored database value - hopefully that makes sense.

Yes, place holders are superb, you can use this in all manner of different ways, but just as you have stated, templates is *probably* the better known way, just be sure to make sure you run the content through a word filter if the content is user generated - just a thought for you there.

As for the example you have given, I don't follow the logic that you are explaining :( Could you elaborate please. Unless you have your logic mixed up and the constants should be assigned after the db call an not before. I do exactly the same thing for my error constants when I do my projects, call a file definitions, get info from db, assign to constants - hey presto!

Either that or I have totally misunderstood you!

Cheers,
MRb

Sp4rkyM4rk




msg:4187801
 4:17 pm on Aug 16, 2010 (gmt 0)

Right, the cosntant is stored in a table like this (but cut down for the example). The field name is on the left, and the value on the right.

id: 1
username: user1
usergroup: EXAMPLE_GROUP


The constant 'EXAMPLE_GROUP' is defined earlier in the file, so when calling the constant within the PHP file without touching the database, this is the output:

This is an example group.


However, when picking up the value from the database using a variable like in the previous example, the actual value is shown without PHP parsing the constant, so it results in 'EXAMPLE_GROUP' being output, rather than its definition.

I'm also interested in placeholders, as it looks much tidier than an if statement in PHP. I see it can also apply to other files including .txt, for example e-mail scripts. Rather than editing the actual PHP file using the CMS, the .txt file can be edited using placeholders like '%s' to represent what would be a variable in PHP.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About
© Webmaster World 1996-2014 all rights reserved