addFilter(new \Twig\TwigFilter('trans', array( $this, 'translate' ))); $twig->addFunction(new \Twig\TwigFunction('plural', array( $this, 'plural' ))); $twig->addFilter(new \Twig\TwigFilter('t', array( $this, 'translate' ))); return $twig->render($template, $variables); } /** * Page rendering based on array data. * * @param $params * * @return bool|string * @throws LoaderError * @throws RuntimeError * @throws SyntaxError */ public function arrayRender($params) { $render = ''; if(is_array($params)){ if(array_key_exists('template', $params)){ $template = $params['template'] . '.html.twig'; $variables = (isset($params['variables'])) ? $params['variables'] : []; $render = $this->twigRender($template, $variables) ?: ''; } else { foreach ($params as $param){ $template = $param['template'] . '.html.twig'; $variables = (isset($param['variables'])) ? $param['variables'] : []; $render .= $this->twigRender($template, $variables) ?: ''; } } } return $render; } /** * Generate a page based on a basic template and content. * * @param $page_content * * @return bool|string * @throws LoaderError * @throws RuntimeError * @throws SyntaxError */ public function baseTemplate($page_content) { if(WebTotemRequest::get('hid')){ WebTotemOption::setSessionOptions(['host_id' => WebTotemRequest::get('hid')]); } $variables['menu_url'] = WebTotem::adminURL('admin.php?page=wtotem'); $page = str_replace(['wtotem', '_'], '', WebTotemRequest::get('page')); $page = $page ?: 'dashboard'; $variables['is_active'][$page] = 'wtotem_nav__link_active'; $variables['page'] = $page; $variables['theme_mode'] = WebTotem::getThemeMode(); $variables['notifications'] = WebTotem::getNotifications(); $variables['current_year'] = date('Y'); $variables['content'] = $page_content; return $this->twigRender('layout.html.twig', $variables); } /** * String translation. * * @param $string * @param array $params * * @return string */ public static function translate($string, array $params = []) { global $locale; $string = ('en_US' !== $locale) ? translate($string, 'wtotem') : $string; if($params){ foreach ($params as $key => $value){ $string = str_replace($key, $value, $string); } } return (string) $string; } /** * @param array $params [single, plural, number] * * usage example * {{ plural({'single' : '%s month', 'plural' : '%s months', 'number' : 1}) }} * * @return string */ public static function plural( array $params): string { $string = _n( $params['single'], $params['plural'], $params['number'],'wtotem' ); $string = str_replace('%s', $params['number'], $string); return (string) $string; } }