Answers.Body: <p>While I know that an extension is a preferred method using the extra templates method works for me in some situations. (I also found it a great way to get my feet wet learning the possibilities of customizing and the API).</p>
  
  <p>To answer why the {if $contributionPageId == "1"} {/if} code didn't work may be you need to wrap those variables in a literal tag.</p>
  
  <p>From the documentation here: <a href="https://docs.civicrm.org/dev/en/latest/framework/templates/" rel="nofollow noreferrer">https://docs.civicrm.org/dev/en/latest/framework/templates/</a></p>
  
  <blockquote>
    <p>One heads up … if your file contains Javascript (as it probably will),
    you will need to start your "extra" custom file with the Smarty
    {literal} tag and the  tag. Then use
    {/literal}{$variable}{literal} if you need to use any Smarty code or
    variables. Finally, end the file with {/literal}.</p>
  </blockquote>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-12-20T20:52:40.203Z
Answers.Text: I tried simplifying the Main.extra.tpl to just contain: {if $contributionPageId == "1"}Hello Workd {/if} and that does not display.  It displays fine without the if statement.  Any ideas?
Answers.UserDisplayName: Rachel
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-12-20T16:11:48.677Z
Answers.LastActivityDate: 2017-12-20T16:11:48.677Z
Answers.UserDisplayName: RayWright
Answers.Body: <p>There are two "better ways" you can try.</p>
  
  <p>First, you can put a template override into a folder with the contribution page ID.  So if the current override is at <code>&lt;overrides&gt;/templates/CRM/Contribution/Form.php</code>, then <code>&lt;overrides&gt;/templates/CRM/Contribution/1/Form.php</code> will only override page ID 1.</p>
  
  <p>However, template overrides aren't very maintainable, and they're pretty hacky.</p>
  
  <p>Instead, you can <a href="https://docs.civicrm.org/dev/en/latest/extensions/" rel="nofollow noreferrer">create an extension</a>.  You can use <a href="https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_buildForm/" rel="nofollow noreferrer">hook_civicrm_buildForm</a> like so:</p>
  
  <pre><code>function myextension_civicrm_buildForm( $formName, &amp;$form ) { 
    if ($formName == 'CRM_Contribution_Form_Main' ){
      CRM_Core_Resources::singleton()-&gt;addScriptFile('org.example.myextension', 'js/myjavascript.js');
    }
  }
  </code></pre>
  
  <p>This technique is covered in greater detail in the <a href="https://docs.civicrm.org/dev/en/latest/framework/resources/" rel="nofollow noreferrer">Resources Reference</a>.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-12-20T21:06:06.507Z
Answers.Text: We'll be testing a bunch of pages, so separate template overrides for each page id would mean too many separate files to manage. Unless I can make the if statement in a civi template work, I'll need to keep my page.tpl.php hack in place until I have time to look at creating an extension - at least this way there is only 1 file to maintain.  Thanks!
Answers.UserDisplayName: Rachel
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-12-20T02:33:38.010Z
Answers.LastActivityDate: 2017-12-20T02:33:38.010Z
Answers.UserDisplayName: Jon G - Megaphone Tech
Answers.Body: <p>Writing an extension is a better way of packaging this type of change.</p>
  
  <p>As Jon pointed out, it's fairly easy with civix to create one and inject the js/css you need</p>
  
  <p>However, it soon becomes a bit of a nightmare to try to handle the contribution page ids and on the top of having to track that id 42 has that lovely towel feature while id 84 has whatever special, you have users that start cloning and replacing and reshuffling stuff...</p>
  
  <p>So assuming it's more than a quick hack, I would create a custom field (eg "layout" + a list of options and modify your extension so it reads this custom field value instead of the id to decide what to inject</p>
  
  <p>And if you think whatever you are changing is generic enough, please consider submit it to our <a href="https://civicrm.org/extensions" rel="nofollow noreferrer">extension directory</a>, and if not, making your repository public on github or somewhere would certainly help someone else facing a problem similar to yours</p>
  
  <p>X+</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-12-20T21:02:48.650Z
Answers.Text: I needed a quick hack to get this up and running, and while my page.tpl.php fix is working, I'd like to keep the hack within CiviCRM. I think I might move it to an extension when there is more time. I am thinking of something along the lines of scanning for active contribution pages and then selecting which elements get added to them - perhaps checkboxes to attach certain elements to specific pages. This way there can be multiple options on multiple pages all managed from one location. It would also need to allow for the input of the text/html/css/js or the file to include. Interesting...
Answers.UserDisplayName: Rachel
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-12-20T06:26:36.223Z
Answers.LastActivityDate: 2017-12-20T06:26:36.223Z
Answers.UserDisplayName: Xavier
Body: <p>Drupal 7 site:</p>
  
  <p>We need to add data to certain donation pages.  For instance, it should appear on id=1 but not on id=2.  This includes some code and js.</p>
  
  <p>I made a Main.extra.tpl and everything works fine when it appears on all pages, but I have not been able to restrict it to be included only on page id=1.</p>
  
  <p>I read that you can do:
  {if $contributionPageId == "yourPageId"} Your custom messaging here {/if} </p>
  
  <p>but wrapping my code in<br>
  {if $contributionPageId == "1"}  {/if} 
  doesn't work.</p>
  
  <p>Is there a better way to do this?</p>
  
  <p>Thanks,
  Rachel</p>
  
  <p><strong>Edit</strong>: For now I've modified Drupal's page.tpl.php with:</p>
  
  <p>if ((arg(0) == 'civicrm' &amp;&amp; arg(1) == 'contribute') &amp;&amp; ($_GET['id'] == '1')):</p>
  
  <p>which does the job - but I still believe there must be a better way.</p>
  
Tags: <drupal><civicontribute><contribution-page>
Title: Adding copy/code to specific donation pages
ThreadText: # Adding copy/code to specific donation pages
  
  (Asked by: username_0 on 2017-12-19)
  
  Drupal 7 site:
  
  We need to add data to certain donation pages. For instance, it should appear on id=1 but not on id=2. This includes some code and js.
  
  I made a Main.extra.tpl and everything works fine when it appears on all pages, but I have not been able to restrict it to be included only on page id=1.
  
  I read that you can do:
  {if $contributionPageId == "yourPageId"} Your custom messaging here {/if}
  
  but wrapping my code in  
  {if $contributionPageId == "1"} {/if}
  doesn't work.
  
  Is there a better way to do this?
  
  Thanks,
  username_0
  
  **Edit**: For now I've modified Drupal's page.tpl.php with:
  
  if ((arg(0) == 'civicrm' && arg(1) == 'contribute') && ($\_GET['id'] == '1')):
  
  which does the job - but I still believe there must be a better way.
  
  **Tags:** drupal, civicontribute, contribution-page
  
  
  ---
  
  ## 3 answers
  
  ### Answer by username_1 on 2017-12-20. Score: 1
  
  While I know that an extension is a preferred method using the extra templates method works for me in some situations. (I also found it a great way to get my feet wet learning the possibilities of customizing and the API).
  
  To answer why the {if $contributionPageId == "1"} {/if} code didn't work may be you need to wrap those variables in a literal tag.
  
  From the documentation here: <https://docs.civicrm.org/dev/en/latest/framework/templates/>
  
  > One heads up … if your file contains Javascript (as it probably will),
  > you will need to start your "extra" custom file with the Smarty
  > {literal} tag and the tag. Then use
  > {/literal}{$variable}{literal} if you need to use any Smarty code or
  > variables. Finally, end the file with {/literal}.
  
  > **Comments:**
  > **username_0**: I tried simplifying the Main.extra.tpl to just contain: {if $contributionPageId == "1"}Hello Workd {/if} and that does not display.  It displays fine without the if statement.  Any ideas?  
  
  ---
  
  ### Answer by username_2 on 2017-12-20. Score: 1
  
  There are two "better ways" you can try.
  First, you can put a template override into a folder with the contribution page ID.  So if the current override is at /templates/CRM/Contribution/Form.php, then /templates/CRM/Contribution/1/Form.php will only override page ID 1.
  However, template overrides aren't very maintainable, and they're pretty hacky.
  Instead, you can create an extension.  You can use hook_civicrm_buildForm like so:
  function myextension_civicrm_buildForm( $formName, &$form ) { 
    if ($formName == 'CRM_Contribution_Form_Main' ){
      CRM_Core_Resources::singleton()->addScriptFile('org.example.myextension', 'js/myjavascript.js');
    }
  }
  
  This technique is covered in greater detail in the Resources Reference.
  
  > **Comments:**
  > **username_0**: We'll be testing a bunch of pages, so separate template overrides for each page id would mean too many separate files to manage. Unless I can make the if statement in a civi template work, I'll need to keep my page.tpl.php hack in place until I have time to look at creating an extension - at least this way there is only 1 file to maintain.  Thanks!  
  
  ---
  
  ### Answer by username_3 on 2017-12-20. Score: 1
  
  Writing an extension is a better way of packaging this type of change.
  
  As Jon pointed out, it's fairly easy with civix to create one and inject the js/css you need
  
  However, it soon becomes a bit of a nightmare to try to handle the contribution page ids and on the top of having to track that id 42 has that lovely towel feature while id 84 has whatever special, you have users that start cloning and replacing and reshuffling stuff...
  
  So assuming it's more than a quick hack, I would create a custom field (eg "layout" + a list of options and modify your extension so it reads this custom field value instead of the id to decide what to inject
  
  And if you think whatever you are changing is generic enough, please consider submit it to our [extension directory](https://civicrm.org/extensions), and if not, making your repository public on github or somewhere would certainly help someone else facing a problem similar to yours
  
  X+
  
  > **Comments:**
  > **username_0**: I needed a quick hack to get this up and running, and while my page.tpl.php fix is working, I'd like to keep the hack within CiviCRM. I think I might move it to an extension when there is more time. I am thinking of something along the lines of scanning for active contribution pages and then selecting which elements get added to them - perhaps checkboxes to attach certain elements to specific pages. This way there can be multiple options on multiple pages all managed from one location. It would also need to allow for the input of the text/html/css/js or the file to include. Interesting...  
  
  ---
  
  