Jump to:

3445 Posts in 1031 Topics by 872 members

Template Questions

SilverStripe Forums » Template Questions » More information on email templates?

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1 2
Go to End
Author Topic: 5476 Views
  • Shauna G
    Avatar
    Community Member
    52 Posts

    More information on email templates? Link to this post

    So I've been working with SilverStripe for a couple of weeks now, working on migrating a static site over. One of the pages, of course, is a contact page that sends an email to the designated recipient. I followed the tutorial over on SSbits to get started with the basic form and have expanded from there. However, I've run into a few obstacles/quirks and have been unable to find any information on them.

    First of all, in the email that is sent (HTML email), output from PhoneNumberField is:
    array ( 'Number' => '1234567890', )
    instead of simply the number (or, as one might expect when using a formatted field, something that better resembles a phone number). Is there a way to either use the PhoneNumberField and have it show the number properly, or use the standard text field and format it in the template to be more readable?

    Secondly, I wanted some bits of static text to show up only if a particular field has been filled in. However, trying to add a template logic block seems to break the email (email doesn't send). Is it possible to have any kind of logic like that within the Email template?

    Finally, does anyone happen know of any contact form tutorials that are more advanced? The SSbits one was great for getting started, but its scope is very limited and a more advanced tutorial (kind of like the "Extending a basic site" tutorial is a more advanced tutorial from the "Building a basic site") would be nice.

  • Willr
    Avatar
    Forum Moderator
    5508 Posts

    Re: More information on email templates? Link to this post

    Welcome Dragonwolf to the forums.

    The PhoneNumberField is only a form field type, its not a data type. If it was a data type then you could have methods for formatting it like $Phone.Format(US) which would format it in the US method. I don't believe there is any built in phone data types so you could either create your own data type which has formatting methods. Have alook at the Decimal for an example of a data type with methods for formatting etc.

    Email should support the full range of SSViewer commands so you defiantly should be able to do ifs, controls etc.

    Want specifically are you looking for in a complex contact form. The forms tutorial would have a bit more information which you could use - http://doc.silverstripe.org/doku.php?id=tutorial:3-forms

  • Shauna G
    Avatar
    Community Member
    52 Posts

    Re: More information on email templates? Link to this post

    Thanks for the information.

    Whether it formats the output in the way a phone number does isn't of huge concern to me. I was mostly wondering if there was a way, as I can't really find much information on it. However, the fact that when you call the value (the same way you call other values) that was input into the field and you get back "'Number' => '1234567890'", then that is a problem, as it's not doing what one would expect, which is output only the value, like other TextFields. Also, from what I can tell, the field itself isn't formatting the numbers, anyway. So effectively, all I'm getting is a shorter text/numeric field.

    As for form complexity, the form I currently have on the static site that I'm converting has the option to chose the preferred method of contact (phone or email), and bases the required field on that (so if email is selected, then an email address has to be provided), and the visual indication changes accordingly. In regards to what the tutorials would have, simply more information on what can be done with both email and forms (hence why I said contact forms, as it would have both), especially a little beyond the "101" level that builds a basic form with an equally basic email would be useful. Even the documentation for "email" doesn't have much (and "email template" is completely empty).

    For email, what about other headers (such as setting the priority level)?

    For forms, what about setting the size property of the text fields (which determines the width of the field itself)?

    These two key things seem to be missing, or at least finding information about how to do either has proven extremely difficult (and considering I've come across a couple unanswered forum posts regarding the size property issue, I'd venture to guess that that one falls under the former).

  • Willr
    Avatar
    Forum Moderator
    5508 Posts

    Re: More information on email templates? Link to this post

    For email, what about other headers (such as setting the priority level)?

    Best method if you can't find something in docs is to look at the API documentation. Theres no built in setPriority function but I'm pretty sure you should be able to use $email->addCustomHeader('X-Priority', '1'); to set it (1 is high, 5 is low). Ive added a quick note the the email page about it http://doc.silverstripe.org/doku.php?id=email#setting_custom_headers

    For forms, what about setting the size property of the text fields (which determines the width of the field itself)?

    You should really style form fields with CSS rather than rely on the size attribute. You can set the max size of a form field by passing the max length value into the constructor - eg for text fields http://api.silverstripe.org/default/TextField.html.

    simply more information on what can be done with both email and forms

    Well what you do with forms is pretty endless, I guess some more examples of the various API features would be good. The email documentation is brief but so is really what you can do with it!

    EDIT: fixed links, I'm going to be so happy when I get round to implementing Preview Post for this forum....

  • Shauna G
    Avatar
    Community Member
    52 Posts

    Re: More information on email templates? Link to this post

    You should really style form fields with CSS rather than rely on the size attribute. You can set the max size of a form field by passing the max length value into the constructor

    Max length is not the same as the size attribute, though, and are, in fact, two completely different attributes. The size attribute in HTML form fields defines the physical size, in pixels (or in a couple of cases, in visible characters), of the field itself. It has nothing to do with the number of characters the field can hold (as is the case with "maxlength"). (On a side note, why the HTML spec uses "size" and not "width" is beyond me.) More information on the "size" attribute of HTML fields can be found at W3Schools. Now, I did happen to stumble on information that shows that the "width" CSS attribute does, in fact, work (or at least is supposed to) on the form fields, which more or less renders my current problem in that regards moot, however, it doesn't change the fundamental misunderstanding of "size" vs "maxlength" and the lack of information on the matter as a whole. As another aside, this did also bring to light that the field IDs of form fields generated from the code have really long IDs...

    Well, at least now, it will be somewhere that one should use the CSS "width" style in place of the HTML "size" attribute for fields, since like I said before, there was an unanswered question about it that came up in my own search, and there will inevitably be more people looking.

    Also, thank you for updating the documentation about the email thing. One of the things I have noticed is that you guys are really good about listening to people, both on and off of these forums and working to address various issues. Good customer service is, sadly, an increasingly rare thing, so keep up the good work.

    I guess some more examples of the various API features would be good.

    One of the pitfalls I've found in community software projects in general is that they tend to be fantastic for really basic users and powerful for advanced users, but leave the intermediate ones somewhere in Limbo. In the case of SS, as an intermediate developer, the API documentation, while certainly thorough and probably useful to those more knowledgeable than I, is a step away from Greek to me. It's basically like "I know it has $Attribute, but how do I access it to make it do what I want?" and, unfortunately, that level of example seems to be missing (and that goes for more than just SS, as I previously mentioned).

    Finally, I still can't seem to get template logic working in my email templates. It works great until I try to add the following:

    <% if $From != email@example.com %>
    <p>Text to appear only if the email isn't the default From in the event of an empty field (as set in _config).</p>
    <% end_if %>

    From what I can gather of the information I've been able to find on template if blocks (which has been pretty much limited to the Template doc, other examples that use logic only have "if $property"), I think the syntax is right, but it doesn't successfully send the email. If I should be able to use such template logic in email templates, what am I doing wrong/missing (it works without the if block)?

  • Willr
    Avatar
    Forum Moderator
    5508 Posts

    Re: More information on email templates? Link to this post

    <% if $From != email@example.com %>
    <p>Text to appear only if the email isn't the default From in the event of an empty field (as set in _config).</p>
    <% end_if %>

    You don't need to use $ when your inside <% ... %> tags so you can just do..

    <% if From != email@example.com %>
    ...

    I think the template parser should be able to handle comparing the email (SSViewer comparisons really only work on simple strings). If removing the $ doesn't work I think you might have to move the logic to the PHP file and using populateTemplate to set a variable like FromExample = true, then in the template you can simple do <% if FromExample %>

    Good points on the size tag. I guess I'm just used to how SS works - it sets the size to the max length as well as the maxlength tag.

    // from TextField...
    'maxlength' => ($this->maxLength) ? $this->maxLength : null,
    'size' => ($this->maxLength) ? min( $this->maxLength, 30 ) : null

    So I guess a suitable enhancement is to have a separate size attribute for the field type. Ticket made - http://open.silverstripe.org/ticket/5077

  • Shauna G
    Avatar
    Community Member
    52 Posts

    Re: More information on email templates? Link to this post

    Still no luck with the != logic. I'll have to look into doing it in the PHP (just thought I'd update).

    Once again, thank you for your help and the ticket.

    On an off-topic question, since I can't quickly find an option to send a private message (and should be getting back to work), who can make updates to the documentation? I'd be more than happy/willing to contribute to filling out the documentation more, but it appears that only certain people can do actual changes (not just comments; which isn't a bad thing, just need to take an extra step, then).

  • Willr
    Avatar
    Forum Moderator
    5508 Posts

    Re: More information on email templates? Link to this post

    Anyone can edit the documentation currently. You will need to register first (link at the bottom on the wiki page) once you register and confirm you should be able to edit pages.

    5476 Views
Page: 1 2
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.