Skip to content

Commit

Permalink
refactor: migrated show categories page to Twig (#2969)
Browse files Browse the repository at this point in the history
  • Loading branch information
thorsten committed Jul 7, 2024
1 parent b3a3aad commit 47eb0d8
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,25 @@
<div class="col-md-8">
<h2 class="pb-4 mb-4 border-bottom">{{ categoryHeader }}</h2>

{{ categoryContent }}
{{ categoryContent | raw }}

<p>{{ categoryLevelUp }}</p>
<p>{{ categoryLevelUp | raw }}</p>
</div>

<div class="col-md-4">
<div class="position-sticky" style="top: 2rem">
<div class="p-4 mb-3 bg-light-subtle rounded border">
[categoryImage]
<img
src="{{ categoryImage }}"
alt="{{ categoryFaqsHeader }}"
width="140"
height="140"
class="rounded img-fluid float-end m-1"
/>
[/categoryImage]
{% if categoryImage is not empty %}
<img src="{{ categoryImage }}" alt="{{ categoryFaqsHeader }}" width="140" height="140"
class="rounded img-fluid float-end m-1">
{% endif %}
<h4 class="fst-italic">{{ categoryFaqsHeader }}</h4>
<p class="mb-0 small">{{ categoryDescription }}</p>
</div>

<div class="p-4 mb-3 bg-light-subtle rounded border">
<h4 class="fst-italic">{{ categorySubsHeader }}</h4>
{{ subCategoryContent }}
{{ subCategoryContent | raw }}
</div>

</div>
Expand Down
78 changes: 40 additions & 38 deletions phpmyfaq/show.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@
use phpMyFAQ\Language\Plurals;
use phpMyFAQ\Link;
use phpMyFAQ\Strings;
use phpMyFAQ\Template\TwigWrapper;
use phpMyFAQ\Translation;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Twig\Extension\DebugExtension;

if (!defined('IS_VALID_PHPMYFAQ')) {
http_response_code(400);
Expand All @@ -46,6 +48,12 @@
$categoryHelper = new CategoryHelper();
$categoryHelper->setPlurals(new Plurals());

$twig = new TwigWrapper(PMF_ROOT_DIR . '/assets/templates');
$twig->addExtension(new DebugExtension());
$twigTemplate = $twig->loadTemplate('./show.twig');

$templateVars = [];

if (!is_null($selectedCategoryId) && isset($category->categoryName[$selectedCategoryId])) {
$faqSession->userTracking('show_category', $selectedCategoryId);

Expand All @@ -71,13 +79,9 @@
if ((is_countable($category->getChildNodes((int) $selectedCategoryId)) ? count($category->getChildNodes((int) $selectedCategoryId)) : 0) !== 0) {
$categoryFaqsHeader = Translation::get('msgSubCategories');
$subCategoryContent = $categoryHelper->renderCategoryTree($selectedCategoryId);
$template->parseBlock(
'mainPageContent',
'subCategories',
[
'categorySubsHeader' => $categoryFaqsHeader
]
);
$templateVars = [
'categorySubsHeader' => $categoryFaqsHeader
];
}
}

Expand All @@ -100,27 +104,21 @@
}

if (!is_null($categoryData->getImage()) && strlen((string) $categoryData->getImage()) > 0) {
$template->parseBlock(
'mainPageContent',
'categoryImage',
[
'categoryImage' => $faqConfig->getDefaultUrl() . '/images/' . $categoryData->getImage(),
]
);
$categoryImage = $faqConfig->getDefaultUrl() . 'content/user/images/' . $categoryData->getImage();
}

$template->parse(
'mainPageContent',
[
'categoryHeader' => Translation::get('msgEntriesIn') . Strings::htmlentities($categoryData->getName()),
'categoryFaqsHeader' => $categoryData->getName(),
'categoryDescription' => Strings::htmlspecialchars($categoryData->getDescription() ?? ''),
'categorySubsHeader' => Translation::get('msgSubCategories'),
'categoryContent' => $records,
'subCategoryContent' => $subCategoryContent,
'categoryLevelUp' => $up
]
);
// Twig template variables
$templateVars = [
... $templateVars,
'categoryHeader' => Translation::get('msgEntriesIn') . Strings::htmlentities($categoryData->getName()),
'categoryFaqsHeader' => $categoryData->getName(),
'categoryDescription' => Strings::htmlspecialchars($categoryData->getDescription() ?? ''),
'categorySubsHeader' => Translation::get('msgSubCategories'),
'categoryImage' => $categoryImage ?? null,
'categoryContent' => $records,
'subCategoryContent' => $subCategoryContent,
'categoryLevelUp' => $up,
];
} else {
$selectedCategoryId = 0;
$faqSession->userTracking('show_all_categories', 0);
Expand All @@ -129,16 +127,20 @@
->setConfiguration($faqConfig)
->setCategory($category);

$template->parse(
'mainPageContent',
[
'categoryHeader' => Translation::get('msgFullCategories'),
'categoryFaqsHeader' => Translation::get('msgShowAllCategories'),
'categoryDescription' => Translation::get('msgCategoryDescription'),
'categorySubsHeader' => Translation::get('msgSubCategories'),
'categoryContent' => $categoryHelper->renderCategoryTree($selectedCategoryId),
'subCategoryContent' => Translation::get('msgSubCategoryContent'),
'categoryLevelUp' => '',
]
);
// Twig template variables
$templateVars = [
... $templateVars,
'categoryHeader' => Translation::get('msgFullCategories'),
'categoryFaqsHeader' => Translation::get('msgShowAllCategories'),
'categoryDescription' => Translation::get('msgCategoryDescription'),
'categorySubsHeader' => Translation::get('msgSubCategories'),
'categoryContent' => $categoryHelper->renderCategoryTree($selectedCategoryId),
'subCategoryContent' => Translation::get('msgSubCategoryContent'),
'categoryLevelUp' => '',
];
}

$template->addRenderedTwigOutput(
'mainPageContent',
$twigTemplate->render($templateVars)
);

0 comments on commit 47eb0d8

Please sign in to comment.