0

Is posible to save SS template variable in database from CMS and after execute it in template?

Okay lets see example: In CMS i have settings where i put social media links and contact informatios. Also in CMS i have module where i create HTML block-s which after that i loop in website.

In that html block i want to put existing $SiteConfig.Email variable.

I Try that but that is rendered in template like $SiteConfig.Email not show real email?

Is this posible to do or i need some extra modification?

Check photo enter image description here

Ivan
  • 5,139
  • 11
  • 53
  • 86

1 Answers1

0

The question you have written makes no sense to me, but I understand the screenshot.

So, SilverStripe renders .ss files with a class called SSViewer. Basically it reads the file as string and then runs it through SSViewer to generate the HTML output.
But, as you saw, the output of variables is not processed.

I can think of 3 ways to get what you want:

  1. Run the variables through SSViewer aswell (in this example, use $RenderedHTMLContent in the template)
class MyDataObject extends DataObject {
  private static array $db = [
    'Title' => DBVarchar::class,
    'HTMLContent' => DBText::class,
  ];

  public function Foobar() { return "hello from foobar"; }

  public function RenderedHTMLContent() {
    $template = \SilverStripe\View\SSViewer::fromString($this->HTMLContent);
    // using $this->renderWith() will allow you access to all things of $this in the template. so eg $ID, $Title or $Foobar. Probably also $SiteConfig because it's global
    return $this->renderWith($template);
    // if you want to add extra variables that are not part of $this, you can also do:
    return $this->renderWith($template, ["ExtraVariable" => "Hello from extra variable"]);
    // if you do not want $this, you can do:
    return (new ArrayData(["MyVariable" => "my value"]))->renderWith($template);
  }
}

Please be aware of the security implications this thing brings. SilverStripe is purposely built to not allow content authors to write template files. A content author can not only call the currently scoped object but also all global template variables. This includes $SiteConfig, $List, .... Therefore a "bad" content author can write a template like <% loop $List('SilverStripe\Security\Member') %>$ID $FirstName $LastName $Email $Salt $Password<% end_loop %> or perhaps might access methods that have file access. So only do this if you trust your content authors

  1. Use shortcodes instead of variables. But I never liked shortcodes, so I don't remember how they work. You'll have to lookup the docs for that.

  2. Build your own mini template system with str_replace.

class MyDataObject extends DataObject {
  private static array $db = [
    'Title' => DBVarchar::class,
    'HTMLContent' => DBText::class,
  ];

  public function Foobar() { return "hello from foobar"; }

  public function RenderedHTMLContent() {
    return str_replace(
       [
         '$SiteConfig.Title', 
         '$SiteConfig.Tagline',
         '$Title',
         '$Foobar',
       ],
       [
         SiteConfig::current_site_config()->Title,
         SiteConfig::current_site_config()->Tagline,
         $this->Title,
         $this->Foobar(),
       ],
       $this->HTMLContent
    );
  }
}
Zauberfisch
  • 3,870
  • 18
  • 25
  • Wow shortcodes i cant beleve how i forget for that??? i Read now doc for shortcodes and i think that can slove my problem. – Ivan Feb 08 '22 at 20:43