Example #1
0
 public function testParseLoopingKey()
 {
     $content = '';
     $content .= '{posts}{title}{/posts}';
     $content .= '{posts}{title}{/posts}';
     $vars = ['posts' => [['title' => 'Dog'], ['title' => 'Cat']]];
     $result = TextParser::parse($content, $vars);
     $this->assertEquals('DogCatDogCat', $result);
 }
Example #2
0
 public function testParseWithFilters()
 {
     $filters = [];
     $filters['upper'] = function ($value) {
         return strtoupper($value);
     };
     $filters['lower'] = function ($value) {
         return strtolower($value);
     };
     $content = '';
     $content .= '{foo} {foo|upper} {foo|lower} ';
     $content .= '{posts}{title}{title|upper}{title|lower}{/posts}';
     $vars = ['foo' => 'Bar', 'posts' => [['title' => 'Dog'], ['title' => 'Cat']]];
     $result = TextParser::parse($content, $vars, ['filters' => $filters]);
     $this->assertEquals('Bar BAR bar DogDOGdogCatCATcat', $result);
 }
Example #3
0
 /**
  * Renders a requested content file.
  * The framework uses this method internally.
  * @param string $name The content view to load.
  * @param array $parameters Parameter variables to pass to the view.
  * @return string
  */
 public function renderContent($name, $parameters = [])
 {
     /*
      * Extensibility
      */
     if (($event = $this->fireEvent('page.beforeRenderContent', [$name], true)) || ($event = Event::fire('cms.page.beforeRenderContent', [$this, $name], true))) {
         $content = $event;
     } elseif (($content = Content::loadCached($this->theme, $name)) === null) {
         throw new CmsException(Lang::get('cms::lang.content.not_found_name', ['name' => $name]));
     }
     $fileContent = $content->parsedMarkup;
     /*
      * Parse basic template variables
      */
     if (!empty($parameters)) {
         $fileContent = TextParser::parse($fileContent, $parameters);
     }
     /*
      * Extensibility
      */
     if (($event = $this->fireEvent('page.renderContent', [$name, $fileContent], true)) || ($event = Event::fire('cms.page.renderContent', [$this, $name, $fileContent], true))) {
         return $event;
     }
     return $fileContent;
 }
Example #4
0
 public function renderForSubscriber($subscriber)
 {
     $parser = new TextParser();
     $data = $this->buildTagData($subscriber);
     $result = $parser->parseString($this->content_html, $data);
     // Inject tracking pixel
     $result = str_replace('</body>', $this->getTrackingPixelImage($subscriber) . PHP_EOL . '</body>', $result);
     return $result;
 }
 protected function generateMigrationCode($upCode, $downCode)
 {
     $templatePath = '$/rainlab/builder/classes/databasetablemodel/templates/migration-code.php.tpl';
     $templatePath = File::symbolizePath($templatePath);
     $fileContents = File::get($templatePath);
     return TextParser::parse($fileContents, ['upCode' => $upCode, 'downCode' => $downCode]);
 }
 public function initVersion($versionType)
 {
     $versionTypes = ['migration', 'seeder', 'custom'];
     if (!in_array($versionType, $versionTypes)) {
         throw new SystemException('Unknown version type.');
     }
     $this->version = $this->getNextVersion();
     if ($versionType == 'custom') {
         $this->scriptFileName = null;
         return;
     }
     $templateFiles = ['migration' => 'migration.php.tpl', 'seeder' => 'seeder.php.tpl'];
     $templatePath = '$/rainlab/builder/classes/migrationmodel/templates/' . $templateFiles[$versionType];
     $templatePath = File::symbolizePath($templatePath);
     $fileContents = File::get($templatePath);
     $scriptFileName = $versionType . str_replace('.', '-', $this->version);
     $pluginCodeObj = $this->getPluginCodeObj();
     $this->code = TextParser::parse($fileContents, ['className' => Str::studly($scriptFileName), 'namespace' => $pluginCodeObj->toUpdatesNamespace(), 'tableNamePrefix' => $pluginCodeObj->toDatabasePrefix()]);
     $this->scriptFileName = $scriptFileName;
 }
 protected function makeFile($filePath, $templateName)
 {
     $path = $this->dstPath . DIRECTORY_SEPARATOR . $filePath;
     if (File::isFile($path)) {
         throw new ApplicationException(Lang::get('rainlab.builder::lang.common.error_file_exists', ['path' => $path]));
     }
     $fileDirectory = dirname($path);
     if (!File::isDirectory($fileDirectory)) {
         if (!File::makeDirectory($fileDirectory, 0777, true, true)) {
             throw new SystemException(Lang::get('rainlab.builder::lang.common.error_make_dir', ['name' => $fileDirectory]));
         }
     }
     $templatePath = $this->templatesPath . DIRECTORY_SEPARATOR . $templateName;
     if (!File::isFile($templatePath)) {
         throw new SystemException(Lang::get('rainlab.builder::lang.common.template_not_found', ['name' => $templateName]));
     }
     $fileContents = File::get($templatePath);
     if (!$fileContents) {
         throw new SystemException(Lang::get('rainlab.builder::lang.common.error_loading_template', ['name' => $templateName]));
     }
     $fileContents = TextParser::parse($fileContents, $this->variables);
     if (@File::put($path, $fileContents) === false) {
         throw new SystemException(Lang::get('rainlab.builder::lang.common.error_generating_file', ['path' => $path]));
     }
     @File::chmod($path);
 }