Skip to content

Commit

Permalink
Fixed templating
Browse files Browse the repository at this point in the history
  • Loading branch information
Flobbo authored and Flobbo committed Mar 26, 2019
1 parent d923a5c commit b383811
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 16 deletions.
16 changes: 16 additions & 0 deletions src/LaravelCM/Models/NewsletterTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,20 @@ class NewsletterTemplate extends Model{
];


public function getTemplateUrlAttribute() {
return url('laravel-cm/' . $this->template_name);
}

public function getTemplateFileUrlAttribute() {
return $this->template_url . '/' . $this->template_name . '.html';
}

public function getTemplatePathAttribute() {
return public_path('laravel-cm/' . $this->template_name);
}

public function getTemplateFilePathAttribute() {
return $this->template_path . '/' . $this->template_name . '.html';
}

}
50 changes: 34 additions & 16 deletions src/LaravelCM/Templates.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,20 @@ public function create(array $data){
return $this->template_db->create($data);
}

/**
* Update Template
* @param array $data
* @return bool
*/
public function update($id, array $data, $return_model = false){
$model = $this->find($id);
if($return_model){
$model->update($data);
return $model;
}
return $model->update($data);
}

/**
* Set relations for templates
* @param type $relations
Expand Down Expand Up @@ -155,16 +169,16 @@ public function compileSass() {
// Set foundation-email imports
__DIR__ . '/../resources/defaults/assets/foundation-emails'
];

$src = resource_path('laravel-cm/'.$this->template.'/assets/scss/'.$this->template.'.scss');

$scss = new ScssCompiler();
$scss->setImportPaths($importPaths);

$scssContent = File::get($src);

$css = trim(preg_replace('/\s+/', ' ', $scss->compile($scssContent)));


//dd($this->disk);
return $this->disk->put($this->template . '/assets/style.css', $css);
}

Expand All @@ -174,11 +188,9 @@ public function compileSass() {
* @return void
*/
public function copyImages() {

$imageFolder = resource_path('laravel-cm/'.$this->template.'/assets/images');
$dest = $this->disk->path($this->template . '/assets');
$dest = $this->disk->path($this->template . '/assets/images');
return File::copyDirectory($imageFolder, $dest);

}

/**
Expand All @@ -204,9 +216,8 @@ public function inlineStyles() {
$results = $crawler->html();

// get the styles

$styles = $stylesheetsHrefs->map(function ($stylesheet) {

$path = $this->template . '/assets/style.css';
return $this->disk->get($path);
})->implode("\n\n");
Expand Down Expand Up @@ -238,13 +249,16 @@ private function generateTemplate(){
$stubPath = resource_path('laravel-cm/default');
$destPath = resource_path('laravel-cm/' . $this->template);

// Rename copied files to template-name
File::copyDirectory( $stubPath, $destPath );
$files = File::allFiles($destPath);
foreach($files as $file) {
if(strpos($file->getFilename(), 'template') !== false) {
$renamePath = $file->getPath() . '/' . str_replace('template', str_slug($this->template), $file->getFilename());
File::move($file->getPathname(), $renamePath);
if(!File::exists( $destPath)) {
// Rename copied files to template-name
File::copyDirectory($stubPath, $destPath);
$files = File::allFiles($destPath);
foreach ($files as $file) {
if (strpos($file->getFilename(), 'template') !== false) {
$filename = config('laravel-cm.use_api') ? str_replace('inky', 'blade', $file->getFilename()) : $file->getFilename();
$renamePath = $file->getPath() . '/' . str_replace('template', str_slug($this->template), $filename);
File::move($file->getPathname(), $renamePath);
}
}
}
return;
Expand All @@ -262,19 +276,22 @@ private function remoteCompiler(array $data){
$viewPath = $this->template . '.views.' . $this->template;

$html = View::make($viewPath, $data)->render();

//Resolve API
$api = resolve(RemoteCompiler::class);
$compiled = $api->compile($html, $this->getResourceFiles());

$this->disk->put($this->template . '/' . $this->template . '.html', $compiled);

$this->copyImages();

return $compiled;
}

private function getResourceFiles(){

$files = File::files(resource_path('laravel-cm/'.$this->template.'/assets/scss'));

$resource_files = [];
foreach($files as $file){
$resource_files[] = [
Expand All @@ -283,6 +300,7 @@ private function getResourceFiles(){
'contents' => File::get($file)
];
}

return $resource_files;
}

Expand Down

0 comments on commit b383811

Please sign in to comment.