1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

XenForo_Phrase class, how to use it?

Discussion in 'XenForo Development Discussions' started by Garani, Jan 12, 2012.

  1. Garani

    Garani Active Member

    I have searched and read about, but couldn't find much on the class and frankly I didn't understood yet what it is for.

    I have a need to change phrases printed depending on errors/variable contents. What you CANNOT do in the templates is to use avariable in the {xen:phrase} area.

    So I startet to look for a way to load up the phrase in PHP and then send it the template just to be printed, but I don't know if the class XenForo_Phrase is what I actually need.

    Any heads up on the matter?
     
  2. Onimua

    Onimua Well-Known Member

    It's likely you're going need to use conditionals to print out your varying errors. XenForo_Phrase is the same as what's used in the templates, like:
    PHP:
    $var = new XenForo_Phrase('hello_there'); // returns 'Hello there'
    What you could do is have a conditional block in the code, and just assign $errorMessage to whatever phrase you need and pass that to the template.
     
  3. Garani

    Garani Active Member

    Have the conditionals in the PHP code is a given. Having the conditionals in the template is something I'd rather prefere not to do. I can update PHP code and revisions with new codes, while templates are not in any kind of revision control, which is a pain, but I understand why it is that way.

    Anyhow, for those who will in the future read this thread, here's how to use XenForo_Phrase:

    PHP:
    $var = new XenForo_Phrase('phrase_code'); // returns the object of the phrase
    $viewParams = array (
        
    'var' => $var,
    );
    return 
    $this->responseView('add_on_ViewPublic_method''template_name'$viewParams);
    In the template_name you have to insert the code:
    HTML:
    {$var}
    or the {xen:raw} command.
     
  4. ragtek

    ragtek Guest

    XF can save phrases,templates,options,etc.. also as files;)
    The xf datawriters handle this, BUT they're limited to xenforo stuff.

    that's why i had to change them
    e.g.
    PHP:
        class Ragtek_DeveloperTools_DataWriter_Template extends
            
    XFCP_Ragtek_DeveloperTools_DataWriter_Template
        
    {
            protected function 
    _getDevOutputDir()
            {
                
    $config XenForo_Application::get('config');
     
                if (
    $this->get('style_id') == && $this->get('addon_id') == $config->development->default_addon) {
     
                    return 
    $this->getOption(self::OPTION_DEV_OUTPUT_DIR);
                }
                else
                {
                    return 
    '';
                }
            }
        }
     
    // model
     
        
    class Ragtek_DeveloperTools_Model_Template extends XFCP_Ragtek_DeveloperTools_Model_Template
        
    {
     
            public function 
    getTemplateDevelopmentDirectory()
            {
     
                
    $config XenForo_Application::get('config');
                if (!
    $config->debug || !$config->development->default_addon) {
                    return 
    '';
                }
     
                return 
    XenForo_Application::getInstance()->getRootDir()
                      . 
    '/' $config->development->directory '/file_output/templates';
            }
        }


    now all the templates from my "default add-on" configured in the config.php will be saved as files if debugmode is enabled.
    you'll see the same behaviour with the phrases, options, styleproperties, admintemplates, emailtemplates,....


    That's it!
    Now you can use git/svn/whatever for version control

    If you work with some other developers, you can use this files and build an small importer, which imports all new phrases,templates,etc... into the system:)


    Hope this helps:)
     

    Attached Files:

    Garani likes this.
  5. Garani

    Garani Active Member

    I am picking up this thread once more. What if you actually need the phrase IN the PHP code.

    Well, you can "render" it:

    PHP:
    $var = new XenForo_Phrase('phrase_code'); // returns the object of the phrase
    $renderedText $var->render(true); // reports back the translated text for the user OR the phrase_code if not found
    That's it, you can now use it in your PHP code.
     
    bittiez likes this.
  6. ragtek

    ragtek Guest

    $foo = new XenForo_Phrase('whateve');
    is enought because of the _toString method in xenforo_phrase;)

    PHP:
        public function __toString()
        {
            return 
    $this->render();
        }
     

Share This Page