Skip to content

Commit

Permalink
Merge pull request #8 from bim-g/layout
Browse files Browse the repository at this point in the history
[DOC] update documentation
  • Loading branch information
bim-g authored Mar 23, 2023
2 parents f8b1230 + 0ce8f2a commit 520ba52
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 18 deletions.
39 changes: 38 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,39 @@
# wepesi-view
Simple view engine, just to share data across pages
Simple view engine, and share data across pages

# Introduction

Create directory where all the view file will loaded. eg: `views`
the class has few method to be used in order to make your view work as expected.
* `assign` : help assign a value to the variable. It has two parameters, the first is the name of the variable and the second parameter is the value of the variable.
* `render` : helps display the contents of your file. Is like a parameter, and it's the name of the file.
Note: All files must be defined in the root directory of the view.
* `setLayout` : help set the layout of the pages.

# Installation
```bash
composer require wepesi/view
```

# Usage
## Without layout

```php
$view_dir = __DIR__."/views";
$view = new \Wepesi\App\View($view_dir);
$view->assign("title","Welcom to the main pages");

$view->render("/art");
```

## With layout
In case you are want to set the layout, you should define the `$contentpages` variable into your layout file where the view content will be applyed.
```php
$view_dir = __DIR__."/views";
$view = new \Wepesi\App\View($view_dir);
$view->assign("title","Home page");

view->setLayout("/layout");
$view->render("/home");
```
No need to specify the extension in case it is a php file.
4 changes: 2 additions & 2 deletions example/example_two.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
$view->assign("title","W3css Courses");

$view->setLayout("/layout");
$view->render("/home");
// $view->render("/contact.php");
// $view->render("/home");
$view->render("/contact.php");
3 changes: 1 addition & 2 deletions example/view/art.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<a href="#" class="w3-left w3-button w3-white">MY ART</a>
<a href="#about" class="w3-right w3-button w3-white">About</a>
</header>

<div class="w3-large w3-center w3-padding w3-teal">View without Layout</div>
<!-- Photo Grid -->
<div class="w3-row">
<div class="w3-half">
Expand All @@ -38,7 +38,6 @@

<!-- End Page Content -->
</div>

<!-- Footer / About Section -->
<footer class="w3-light-grey w3-padding-64 w3-center" id="about">
<h2>About</h2>
Expand Down
2 changes: 1 addition & 1 deletion example/view/contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<p><i class="fa fa-envelope-o w3-text-teal w3-xlarge"></i>  [email protected]</p>
</div>
<div class="w3-col m7">
<form class="w3-container w3-card-4 w3-padding-16 w3-white" action="/action_page.php" target="_blank">
<form class="w3-container w3-card-4 w3-padding-16 w3-white" action="https://github.com/bim-g/wepesi-view" target="_blank">
<div class="w3-section">
<label>Name</label>
<input class="w3-input" type="text" name="Name" required>
Expand Down
2 changes: 1 addition & 1 deletion example/view/layout.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
<a href="#" class="w3-bar-item w3-button">Search</a>
</div>
</div>
{{content}}
<?=$contentpages?>
<!-- Footer -->
<footer class="w3-container w3-padding-32 w3-theme-d1 w3-center">
<h4>Follow Us</h4>
Expand Down
2 changes: 0 additions & 2 deletions index.php

This file was deleted.

22 changes: 13 additions & 9 deletions src/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,29 @@ public function __construct(string $view_location)
public function assign(string $key,$value){
$this->data[$key] = $value;
}

public function render(string $file_location){
$layoutContent = $this->layoutContent();
$viewContent = $this->renderOnlyView($file_location);
/**
*
*/
public function render(string $view_file){
if($this->layout){
print(str_replace("{{content}}",$viewContent,$layoutContent));
print($this->renderLayout($view_file));
}else{
print($viewContent);
print($this->renderOnlyView($view_file));
}
}

public function setLayout(string $layout){
$this->layout = $this->checkFileExtension($this->view_location.$layout);
}
protected function layoutContent(){
if($this->layout)

protected function renderLayout(string $view){
if($this->layout && is_file($this->layout))
{

$this->exception($this->layout);
foreach($this->data as $key=>$value){
$layout_data = $this->data;
$layout_data['contentpages'] = $this->renderOnlyView($view);
foreach($layout_data as $key=>$value){
$$key = $value;
}
ob_start();
Expand Down

0 comments on commit 520ba52

Please sign in to comment.