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

what is the best way to load an theird part library?

Discussion in 'XenForo Development Discussions' started by Ajaxboy, Feb 12, 2011.

  1. Ajaxboy

    Ajaxboy Active Member

    what is the best way to load a third party library in the head of the page?

    I can see someone responding saying by adding it to a template.

    But I am looking for a dynamic way done through php.

    eg: somethign like Xenforo_Template_html::addToHead(); (that doesn't exist, but it is an example)

    Just trying to find the simplest within xenforo

    ps: feel free to fix the spelling on the title :), I wasn't awake apparently
  2. Kier

    Kier XenForo Developer Staff Member

    The templates are templates because they are intended to be edited in order to achieve this kind of modification. If the templates were intended to remain strictly unchanged, we could have left them hard-coded in PHP and not expended vast amounts of effort providing a templating system.

    Therefore, the answer is, for now at least, add the library to the appropriate template.
  3. Ajaxboy

    Ajaxboy Active Member

    I dislike the response, but rightfully said. In no way it would be hard-coded, that is really a strong word. "strictly unchanged" it is also a strong word. Very strong. I have a class dynamically return the relative path of a scrip tag. I use templates myself that loop through the head to "deposit" any residual gathered toward the head tag through a single variable. In no way I don't believe this is considered "hard-coded" it is all the opposite. Sounds to me you are using strong words to avoid dealing with it. Which is very understandable. Though I need this, people ask for things because they need them.

    I am not very experienced with integrations (ehh a little bit), but I am experienced buildings shopping carts, frameworks, and applications in general.

    Not many would need this kind of power, having already everything that the system does. I am sure some of things I am trying to load the system can already do, but I am experimenting and reaching the flexibility of xenforo. I am a hardcore developer what are you gonna do?... he .. and I am trying to load my favorite tools into xenforo to see what kind of fancy stuff I can do on it you can say. I built a js ajax framework that you can relate with jquery but not exactly, but you should get the idea from there (is what I am trying to load). Using templates or even an addon to reach the head tag (with all the respect) is ridiculous, doesn't mean I wouldn't do it, but it kind alienates (some) developers you should know that. I rather think this is a very generic tool, for such purpose.

    From all of the said above, just the bold part above is what I need without having to actually create or edit a template or create an addon. Thank You.

    Oh btw, why do I have so much trouble editing a template you ask?... I don't.... I am thinking distribution the whole time. I don't want people to deal extra template modifications and the like when an addon is distributed, etc. Should be straight forward.
  4. Kier

    Kier XenForo Developer Staff Member

    If you want to include a javascript library in the head from within another template, that is very straightforward.

    Simply use the <xen:require ... /> tag, like this:
    <xen:require js="path/to/javascript.js" />
    This may be used as frequently as you like, from any template in your add-on.
  5. Ajaxboy

    Ajaxboy Active Member

    This is the part you don't understand.. the JavaScript is actually generated by a php class. I can't manually take and put it there. Well technically I can, but now that is what can be considered hard-coded (kind of exactly what I am trying to avoid).
  6. Ajaxboy

    Ajaxboy Active Member

    basically all you need is a template like this

    <xen:if is="{$head_var}">
    <xen:foreach loop="$head_var" value="$head">
    and .. a php function that stacks input into an array, that should do it.
  7. ragtek

    ragtek Guest

  8. Ajaxboy

    Ajaxboy Active Member

    Isn't that a mod? i was told there was not such functionality. Explain?

    Does that mean is coming soon ? :)
  9. ragtek

    ragtek Guest

    I mean there's already the part you requested in the template.

    And mike wrote
    So it SHOULD be already available.
  10. Ajaxboy

    Ajaxboy Active Member

    Nope it didn't work,.

    I did see the PAGE_CONTAINER template on there though. I am trying to avoid editing templates that are simply unnecessary to edit.
  11. Ajaxboy

    Ajaxboy Active Member

    so can you add this or what
  12. Kier

    Kier XenForo Developer Staff Member

    The functionality already exists on line 18 of the PAGE_CONTAINER template:
    <xen:if is="{$head}"><xen:foreach loop="$head" value="$headElement">{xen:raw $headElement}</xen:foreach></xen:if>
  13. EQnoble

    EQnoble Well-Known Member

    on this note..

    Would it be OK practice to create a new template called lets say: "custom_includes" entering in all the necessary addon libraries I require using <xen:include template="path/to/jscript.js" /> for as many libraries as I need and then in the page container template only need to make one edit: <xen:include template="custom_includes" /> ?
  14. ragtek

    ragtek Guest

    that's what i'm doing for my add-ons.

    i wrote this already in post 7:p

    BUT how can we add own code to this?

    I know only the way with <xen:container.... which can only be used in templates.
    How can i add own elements from php code, without editing templates?
  15. Shadab

    Shadab Well-Known Member

  16. ragtek

    ragtek Guest

  17. Shadab

    Shadab Well-Known Member

    front_controller_pre_view will work. But I think container_public_params is more appropriate.
    This works for me:
    $containerParams['head']['customParam'] = '<!-- FooBar -->';
    Just choose a parameter name that's not used in any template. A head parameter set in a template always takes precedence over a parameter by the same name defined anywhere else, in code.
    Kier and ragtek like this.
  18. Ajaxboy

    Ajaxboy Active Member

    In case anybody ever needs this, this worked: (although it was not the solution I was looking for, but it will do :) )

    (For some reason none of the above example worked)

    first add an event: template_hook

        public static  function hook_me_up_baby($name $contents ,array $params,XenForo_Template_Abstract $template)
    $name=='page_container_head') {

    $contents .= "\t<!--Foo-->";

    I wanted to do it without requirement of a template.. just a simple built in helper.. etc.. but seems like it works out

    Why a helper would of been better you ask?..... Well just see how long it took, at least 2 to 4 days.. with a helper it would of have taken just 5 min max... that is why I like to keep things simple.. :D. No need to create a whole house, just to spend one night in it.. does that make sense :oops:.

Share This Page